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?

7
  • 8
    CWG253. Commented Jun 19, 2016 at 17:18
  • 1
    @T.C. stackoverflow.com/questions/29683381/… Commented Jun 19, 2016 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… Commented Jun 19, 2016 at 18:15
  • Also related to Is an empty class usable as a constexpr variable without an intializer or explicit default constructor? Commented Jun 20, 2016 at 12:58
  • @Columbo I would happily pay for a tool like that, LOL. Commented Aug 29, 2019 at 22:50

1 Answer 1

42

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wording ignored also by VC++2015

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.