对于exercice,我所著的C++ 有点类不能被强制转换为其他类型比一般的参数T:歧义与COUT <<和施放C++
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;
};
这里是主要的:
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;
}
那么,为什么第二个cout不起作用?这应该是工作,因为我删除了所有其他可能的转换比int。感谢帮助。回到这可能有助于了解:Prevent an object to be casted to any but one type C++
[删除功能参与重载解析](http://stackoverflow.com/questions/14085620/why-do-c11-deleted-functions-participate -in-超负荷分辨率)。 – chris 2015-04-04 19:28:08