模板库中的重载运算符 class
Overload operator in template base class
我有以下代码:
#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
如果我在模板基础 class operator=
中使用关键字 virtual
然后它编译但我看不到任何输出,所以我没有调用正确的方法。
奇怪的是,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。明智地在评论中指出,虽然这段代码可以编译,但它被破坏了,因为默认生成的复制赋值运算符仍然存在。因此,在派生的 class 中,我们应该明确定义复制赋值:
Der& operator=(const Der& o)
{
Base<Der...>::operator=(o);
return *this;
}
我有以下代码:
#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
如果我在模板基础 class operator=
中使用关键字 virtual
然后它编译但我看不到任何输出,所以我没有调用正确的方法。
奇怪的是,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。明智地在评论中指出,虽然这段代码可以编译,但它被破坏了,因为默认生成的复制赋值运算符仍然存在。因此,在派生的 class 中,我们应该明确定义复制赋值:
Der& operator=(const Der& o)
{
Base<Der...>::operator=(o);
return *this;
}