假设我有
#include <string>
class A
{
public:
template<class T>
operator T();
A child();
};
void f()
{
A a;
std::string s1 = a; // ok
std::string s2 = a.child(); // error (line 34)
s1 = a; // error (line 36)
s2 = a.child(); // error (line 37)
}
Run Code Online (Sandbox Code Playgroud)
std :: string构造函数可以使用char*或std :: string引用,这就是赋值不明确的原因.但是为什么我的编译器(VC++ 10)抱怨第二个赋值而不是第一个赋值?
我正在寻找一种方法来优先使用模板转换运算符而不是重载的构造函数.
我收到以下错误:
1>------ Build started: Project: Plasma4Test, Configuration: Debug Win32 ------
1> Plasma4Test.cpp
1>d:\bitbucket\vx\projects\plasma4test\plasma4test.cpp(34): error C2440: 'initializing' : cannot convert from 'A' to 'std::basic_string<_Elem,_Traits,_Ax>'
1> with …Run Code Online (Sandbox Code Playgroud) c++ constructor return-value local-variables conversion-operator
这怎么可能是有效的C++?
void main()
{
int x = 1["WTF?"];
}
Run Code Online (Sandbox Code Playgroud)
在VC++ 10上,这个编译并在调试模式下x,语句之后的值为84.
这是怎么回事?