c++ - 模板基类中的重载运算符

标签 c++ crtp

我有以下代码:

#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

如果我在模板基类 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.明智地在评论中指出,虽然这段代码可以编译,但它被破坏了,因为默认生成的复制赋值运算符仍然存在。因此,在派生类中我们应该显式定义复制赋值:

Der& operator=(const Der& o)
{
    Base<Der...>::operator=(o);
    return *this;
}

关于c++ - 模板基类中的重载运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52803895/

相关文章:

c++ - 在 Visual Studio 2012 C++ 项目中嵌入 Python

c++ - 重置流的状态

c++ - 将 crtp 与公共(public)接口(interface)类一起使用时,如何避免虚拟调用开销?

CRTP 的 C++ 编译器错误

c++ - 您可以使用 CRTP 和以接口(interface)作为参数的函数吗?

c++ - 隐式转换被认为是一个坏概念吗?

c++ - 默认参数有什么问题?

c++ - 为什么 std::search 需要转发迭代器

python - 如何在python中实现CRTP功能?

c++ - 使用具有相同成员函数名称的 CRTP