uninitialized const

fredoverflow picture fredoverflow · Nov 11, 2011 · Viewed 16.7k times · Source

This compiles perfectly fine with the current MSVC compiler:

struct Foo
{
} const foo;

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;

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

Answer

Alok Save picture Alok Save · Nov 11, 2011

The C++03 Standard:

8.5 [dcl.init] paragraph 9

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.

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