For an exercice, I writed a little class in C++ that cannot be casted to other types than the generic parameter T:
template <class T>
class pure
{
public:
pure(T const& attr)
{
this->attr = attr;
}
~pure()
{
}
T& operator=(T const& attr)
{
return attr;
}
operator T()
{
return attr;
}
template<typename U>
operator U() = delete;
private:
T attr;
};
And here is the main :
int main()
{
pure<int> i = 15;
//This won't compile, like I wanted
//All the casts to other types than int won't compile
cout << (short)i;
//BUT when I'm using mye pure object without a cast, I get a lot of ambiguities errors
cout << i;
system("PAUSE");
return 0;
}
So, why the second cout doesn't work ? This should be work because I deleted all other possible casts than int. Thanks for help. Refrring to this may help to understand: Prevent an object to be casted to any but one type C++