我有以下代码:
#include <iostream>
#include <cstring>
#define THIS(X) static_cast<X*>(this)
template<typename T>
class Base {
public:
Base() {
}
virtual ~Base() {
}
void foo() {
THIS(T)->a[0] = 1;
}
T& operator=(const T& o) {
std::cout <<"operator="<<std::endl;
memcpy(THIS(T)->a, o.a, THIS(T)->size);
return static_cast<T&>(*this);
}
};
template<typename T>
class Der1: public Base<Der1<T> > {
private:
T* a;
unsigned int size;
public:
Der1(int d) {
a = new T[d];
size = d;
}
virtual ~Der1() {
delete[] a;
}
using Base<Der1<T> >::operator=;
friend class Base<Der1<T> > ;
};
template<typename T, unsigned int EL>
class Der2: public Base<Der2<T, EL> > {
private:
T a[EL];
unsigned int size;
public:
Der2() {
size = EL;
}
virtual ~Der2() {
}
using Base<Der2<T, EL> >::operator=;
friend class Base<Der2<T, EL> > ;
};
int main() {
Der1<double> a(5);
Der2<double, 10> b;
a.foo();
b.foo();
Der2<double, 10> c;
Der1<double> d(5);
c = b;
a = d;
return 0;
}
我编译时收到此错误:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:48:7: error: ‘constexpr Der2<double, 10>& Der2<double, 10>::operator=(const Der2<double, 10>&)’ cannot be overloaded
class Der2: public Base<Der2<T, EL> > {
^~~~
../main.cpp:23:5: error: with ‘T& Base<T>::operator=(const T&) [with T = Der2<double, 10>]’
T& operator=(const T& o) {
^~~~~~~~
../main.cpp:31:7: error: ‘constexpr Der1<double>& Der1<double>::operator=(const Der1<double>&)’ cannot be overloaded
class Der1: public Base<Der1<T> > {
^~~~
../main.cpp:23:5: error: with ‘T& Base<T>::operator=(const T&) [with T = Der1<double>]’
T& operator=(const T& o) {
^~~~~~~~
make: *** [subdir.mk:20: main.o] Error 1
如果我在模板基类virtual
中使用关键字operator=
,则它可以编译,但看不到任何输出,因此我没有调用正确的方法。
答案 0 :(得分:1)
奇怪的是,clang接受您的代码。这对gcc和clang都有效:
Base& operator=(const Base& o) {
std::cout << "operator=" << std::endl;
memcpy(self().a, o.self().a, self().size);
return *this;
}
T& self() {
return static_cast<T&>(*this);
}
const T& self() const {
return static_cast<const T&>(*this);
}
顺便说一句,virtual
析构函数在这里看起来很奇怪:您正在将编译时多态(通过CRTP)与运行时多态(通过virtual
)混合在一起。也许您不需要它。
编辑。
@ n.m。在注释中明智地指出,尽管此代码可以编译,但是由于默认生成的副本赋值运算符仍然存在,所以它已损坏。因此,在派生类中,我们应该明确定义副本分配:
Der& operator=(const Der& o)
{
Base<Der...>::operator=(o);
return *this;
}