Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC accepts `constexpr struct {} s;` but Clang rejects it. Who is correct?

The following code compiles fine with GCC:

constexpr struct {} s; 

But Clang rejects it with the following error:

error: default initialization of an object of const type 'const struct (anonymous struct at …)' without a user-provided default constructor

I've tested all versions of GCC and Clang that I was able to find at https://gcc.godbolt.org/. Each version of GCC accepts the code and each version of Clang rejects it.

I wonder which compiler is correct in this case?
What does the standard say about this?

like image 268
HolyBlackCat Avatar asked Jun 19 '16 16:06

HolyBlackCat


1 Answers

Clang adheres to the following passage in [dcl.init]/7:

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.

This wording is defective and hence ignored by GCC (and, as of v3.9, also by Clang).
The above quote differs from core issue 253's due to N2762 moving/adjusting paragraph 9 as cited.

like image 70
Columbo Avatar answered Sep 21 '22 09:09

Columbo