I'm a little new to C++ templating, and I'm having trouble understanding this behaviour. Basically, I have a templated class, and I want to define the = operator between the class and the same class with any template parameter. The problem is that the overloaded version of the = operator is only being used when the two instances of the class have distinct template parameters. It's not being called when the same class types are being used.
I've isolated the problem as much as I can in the following code:
include <iostream>
using namespace std;
template <class T>
class A
{
public:
template <class X> A<T>& operator=(const A<X>& a);
};
template <class T>
template <class X>
A<T>& A<T>::operator=(const A<X>& a)
{
cout << "A's operator= called" << endl;
return *this;
}
int main()
{
A<double> ad1;
A<double> ad2;
A<float> af;
cout << "Assigning an A<double> to an A<float>:" << endl;
ad1 = af;
cout << "Assigning an A<double> to an A<double>:" << endl;
ad1 = ad2;
return 0;
}
Which has the output:
Assigning an A<double> to an A<float>:
A's operator= called
Assigning an A<double> to an A<double>:
Is this the way C++ is supposed to work? I'm using the GNU C++ compiler. Shouldn't the compiler recognise that the operator='s template parameters can be made to match the requested operation? Or do I just have to define an operator= for the specific case of when the template parameters are equal?