以下代码可以使用 GCC 正常编译:
constexpr struct {} s;
但 Clang 拒绝它并出现以下错误:
error: default initialization of an object of const type 'const struct (anonymous struct at …)' without a user-provided default constructor
我已经测试了我在 https://gcc.godbolt.org/ 找到的所有 GCC 和 Clang 版本。 . GCC 的每个版本都接受该代码,而 Clang 的每个版本都拒绝它。
不知道在这种情况下哪个编译器是正确的?
标准对此有何规定?
最佳答案
Clang 遵循 [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.
这个措辞是defective和 hence ignored by GCC (并且,从 v3.9 开始,也由 Clang 提供)。
由于 N2762,以上引用与核心问题 253 不同。移动/调整引用的第 9 段。
关于c++ - GCC 接受 `constexpr struct {} s;` 但 Clang 拒绝它。谁是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909549/