Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uninitialized const

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?

like image 652
fredoverflow Avatar asked Nov 11 '11 10:11

fredoverflow


People also ask

Is it necessary to initialize const variables?

A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type. Constant variables can be declared for any data types, such as int , double , char , or string .

How do you initialize a constant data member in a class in C++?

To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.


2 Answers

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.

like image 186
Alok Save Avatar answered Sep 21 '22 08:09

Alok Save


[2003: 8.5/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. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

And:

[n3290: 8.5/11]: If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2._ —end note_ ]

[n3290: 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

So MSVC is more permissive here than both standards mandate.

like image 32
Lightness Races in Orbit Avatar answered Sep 21 '22 08:09

Lightness Races in Orbit