当我尝试使用 g++ 编译以下内容时:
const int zero;
int main()
{
return 0;
}
我收到关于
uninitialized const 'zero'
的错误消息.我认为全局变量默认初始化为 0 [1] ?为什么这里不是这种情况?VS 编译得很好。
[1] 例如,见 https://stackoverflow.com/a/10927293/331785
最佳答案
我的 gcc 稍微详细一点:
$ g++ zeroconst.c
zeroconst.c:1:11: error: uninitialized const ‘zero’ [-fpermissive]
我们看到
-fpermissive
选项将允许编译。在 uninitialized const 上看到这个问题引用 C++ 标准(问题是 C++ 特定的)。
如 GCC wiki 所引用:
As mandated by the C++ standard (8.5 [decl.init], para 9 in C++03, para 6 in C++0x), G++ does not allows objects of const-qualified type to be default initialized unless the type has a user-declared default constructor. Code that fails to compile can be fixed by providing an initializer...
关于gcc - 为什么 gcc 不会编译未初始化的全局常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568830/