45

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?

Columbo
  • 60,038
  • 8
  • 155
  • 203
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 8
    [CWG253](http://wg21.link/CWG253). – T.C. Jun 19 '16 at 17:18
  • 1
    @T.C. http://stackoverflow.com/questions/29683381/const-t-works-const-t-fails-when-t-is-a-non-pod#comment47503814_29683381 – Columbo Jun 19 '16 at 17:23
  • 6
    @T.C. Can't believe what a f!cking ordeal it is to find the source of changes in the standard. I just spent 15 minutes using a bisection method to go through the git repo and Pete Becker's working drafts (fortunately including changes markup!), then going through each paper and core issue listed in the corresponding editor's report until I finally found what happened to [dcl.init]p7. One day I'll write something up to automatize that… – Columbo Jun 19 '16 at 18:15
  • Also related to [Is an empty class usable as a constexpr variable without an intializer or explicit default constructor?](http://stackoverflow.com/q/33813929/1708801) – Shafik Yaghmour Jun 20 '16 at 12:58
  • @Columbo I would happily pay for a tool like that, LOL. – Marco Bonelli Aug 29 '19 at 22:50

1 Answers1

41

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.

Columbo
  • 60,038
  • 8
  • 155
  • 203