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

雨燕双飞 提交于 2019-12-10 12:28:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37909549/gcc-accepts-constexpr-struct-s-but-clang-rejects-it-who-is-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!