greywolf82
greywolf82

Reputation: 22173

Overload operator in template base class

I've got the following code:

#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;
}

When I compile I receive this error:

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

If I use the keyword virtual in template base class operator= then it compiles but I can't see any output, so I'm not calling the right method.

Upvotes: 1

Views: 340

Answers (1)

Evg
Evg

Reputation: 26272

Curiously, clang accepts your code. This works in both, gcc and 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);
}

BTW, a virtual destructor looks odd here: you're mixing compile-time polymorphism (via CRTP) with run-time polymorphism (via virtual). Probably, you don't need it.

Edit.

As @n.m. judiciously pointed in the comment, although this code compiles, it's broken because default-generated copy assignment operator is still there. So, in the derived class we should define copy assignment explicitly:

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

Upvotes: 1

Related Questions