c++ - GCC 接受 `constexpr struct {} s;` 但 Clang 拒绝它。谁是正确的?

标签 c++ gcc clang language-lawyer constexpr

以下代码可以使用 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.

这个措辞是defectivehence ignored by GCC (并且,从 v3.9 开始,也由 Clang 提供)。
由于 N2762,以上引用与核心问题 253 不同。移动/调整引用的第 9 段。

关于c++ - GCC 接受 `constexpr struct {} s;` 但 Clang 拒绝它。谁是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909549/

相关文章:

c++ - dynamic_cast 何时因隐藏符号而失败?

c++ - C++中缩小和截断之间的区别?

c++ - g++ 不会在 Snow Leopard 下编译 std=c++0x

缺少 Python.h header

c++ - 在 Python 中创建 C++ 类的实例并在其上调用方法

c++ - 编译器在编译时如何检测数字溢出?

Clang:不要优化特定功能

c++ - asin 使用 Clang 在不同的平台上产生不同的答案

macos - "clang"和 "Apple clang"有什么区别?

c++11 - Apple clang-703.0.29 如何在 C++1x 支持方面映射回 clang 版本?