Vin*_*lco 12 c++ visual-studio-2010 visual-c++ c++11 visual-studio-2012
为什么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;
}
Run Code Online (Sandbox Code Playgroud)
添加explicit关键字不会改变gcc或clang的内容.产生的错误消息是:
error C2676: binary '||' : 'S' does not define this operator or a
conversion to a type acceptable to the predefined operator
Run Code Online (Sandbox Code Playgroud)
如果禁止隐式类型转换为布尔值,编译器必须检查是否存在运算符 || 定义需要一个布尔值。(希望如此!)未定义,因为它会破坏快捷方式。因此,他必须检查是否定义了一个转换运算符,该运算符为其提供了为全局 || 运算符定义的内容。您可以通过向 bool、BOOL 或 int (无论如何都是相同的^^)\xe2\x80\xa6 添加转换运算符来解决这个问题吗?
\n\noperator bool() {return this != null && this != 0xcccccccc;}
顺便说一句,这只是一个黑客,最好提供一个逻辑上有意义的转换。
\n\n我确信在调试模式下为 0xcccccccc 标记未初始化内存定义了一个常量,但我不知道。
\n| 归档时间: |
|
| 查看次数: |
1401 次 |
| 最近记录: |