I have the following code:
#include <stdio.h>
template<int A>
class Thing
{ // 5
public:
Thing() :
data(A) {
}
template<int B>
Thing &operator=(const Thing<B> &other) {
printf("operator=: A = %d; B = %d\n", A, B);
printf("this->data = %d\n", data);
}
private:
int data;
};
int main() {
Thing<0> a, b;
Thing<1> c;
a = b;
a = c;
c = b;
return 0;
}
I need to specialize Thing<A>::operator=
for A == B
. I have tried this:
template<int B>
template<int A>
Thing<A> &Thing<A>::template operator=(const Thing<A> &other) { // 23
printf("operator= (specialized): A = %d; B = %d; A %c= B\n", A, B, (A == B) ? '=' : '!');
printf("this->data = %d; other.data = %d\n", data, other.data);
}
However, I receive compile errors with g++:
23: error: invalid use of incomplete type ‘class Thing<B>’
5: error: declaration of ‘class Thing<B>’
I have tried using an if(A == B)
in operator=
without a specialization. However, I receive errors for accessing the private member data
, which I need to access where A == B
.
How can I properly specialize my member function template operator=
of the class template Thing
?