未初始化的const

未初始化的const

问题描述:

这与现有的MSVC编译器完全一致:

This compiles perfectly fine with the current MSVC compiler:

struct Foo
{
} const foo;

但是,无法使用当前的g ++编译器进行编译:

However, it fails to compile with the current g++ compiler:

error: uninitialized const 'foo' [-fpermissive]
note: 'const struct Foo' has no user-provided default constructor

如果我自己提供一个默认构造函数,它的工作原理:

If I provide a default constructor myself, it works:

struct Foo
{
    Foo() {}
} const foo;

这是MSVC的另一种情况是过于宽松,还是g ++太严格了?

Is this another case of MSVC being too permissive, or is g++ too strict here?

C ++ 03标准

8.5 [dcl.init]段落9


如果没有为某个对象指定初始值设定项, (可能是cv限定的)非POD类类型(或其数组),对象将被默认初始化;如果对象是const限定类型,底层类类型应该有一个用户声明的默认构造函数。

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor.

在gcc中的错误似乎是完全有效的。

From the above the error in gcc seems to be perfectly valid.