The problem with this conversion is:
template<class T1> operator T1() { return T1(); }
lies in the fact that it converts B to anything, so they will be compiled:
typedef B<int> B1;
typedef B<float> B2;
B1 b1;
B2 b2 = b1;
int x = b1;
std::string s = b2;
When looking at your test case, these examples require the assignment operator not to copy the constructor:
b1 = b2; // B2 => B1
b2 = b1; // B1 => B2
So, if you define your class with assignment operators like this, your test cases should work:
template<class T>
struct B
{
B(){}
B& operator=(B&){ return B<T>();};
template<class T1> B(B<T1> const& ) {}
template<class T1> B& operator=(B<T1>&){return B<T>();};
};
source
share