The following code compiles with some versions with GCC and some versions of Clang (see below which versions).
struct my_struct {};
int main(int argc, char** argv) {
const my_struct my_object;
return 0;
};
Compile with :g++ clang_error.cpp
and clang++ clang_error.cpp
. Where I had g++ at 4.8.4 and clang++ at 3.6.0.
The error message is:
clang_error.cpp:7:19: error: default initialization of an object of const type 'const my_struct' without a user-provided default constructor
const my_struct my_object;
^
clang_error.cpp:7:28: note: add an explicit initializer to initialize 'my_object'
const my_struct my_object;
^
= {}
1 error generated.
Versions affected
Using the Compiler Explorer here, I can see that GCC up to 4.5.4 is affected. Clang is affected up to 3.9.0.
Question
My question is: What does the C++ standard say about this? Ideally, I'd care about C++14 but I'm not picky there.
Is the above example code standard-conformant?
What I found so far
I found the following in Draft N3797 of C++14.
§ 7.1.6.1 The cv-qualifiers [dcl.type.cv]
2 [ Note: Declaring a variable const can affect its linkage (7.1.1) and its usability in constant expressions (5.19). As described in 8.5, the definition of an object or subobject of const-qualified type must specify an initializer or be subject to default-initialization. — end note ]§ 8.5
7 To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor (12.1) for T is called (and the initialization is ill-formed if T has no default constructor or overload resolution (13.3) results in an ambiguity or in a function that is deleted or inaccessible from the context of the initialization);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.