Class X contains 2 pieces of data. The templated assignment operator accepts any type and assigns it to member 'd'. However I still want copy assignment to work properly. In MSVC 2010 the line 'b = a;' calls the template assignment operator not the copy assignment. How can I overload assignment to distinguish properly or have the template assignment distinguish internally?
class X
{
public:
X() : x(0), d(0.0) {}
X(X const & that) { x = that.x; d = that.d; }
X& operator=(X const & that) { x = that.x; d = that.d; }
template<typename T>
X& operator=(T && y) {
//if (T is X&)
// operator=(static_cast<X const &>(that));
//else
// d = y;
return *this;
}
int x;
double d;
};
void f()
{
X a;
X b;
a = 5;
a = 3.2;
b = static_cast<X const &>(a); // calls copy assignment
b = a; // calls template assignment
}