为什么 Visual Studio 2010 和 Visual Studio 2012 无法编译此代码?
Codepad.org、Xcode、gcc、LLVM、Clang 都没有问题,但 Visual Studio 会大便:
struct S {
template <class T> inline operator T () const { return T (); }
};
int main () {
// NOTE: "S()" denotes construction in these examples
struct F {
void operator() (bool) { }
static void toint (int) { }
static void tostr (char const*) { }
};
bool b1 = S (); // Okay
bool b2 (S ()); // Okay
F () (S ()); // Okay
F::toint (S ());// Okay
F::tostr (S ());// Okay
S () || false; // Error: error C2676: binary '||' : 'vf::S' does
// not define this operator or a conversion to a type
// acceptable to the predefined operator
return 0;
}
添加explicit
关键字不会改变 gcc 或 clang 的任何事情。产生的错误信息是:
error C2676: binary '||' : 'S' does not define this operator or a
conversion to a type acceptable to the predefined operator