c++ - 不透明和匿名枚举声明如何符合标准要求?

标签 c++ c++11 enums language-lawyer anonymous-types

我在 N3936(条款 7.2.2)中读到“在范围枚举的声明中不应省略可选标识符”,所以我尝试了以下代码 (嵌入的评论试图解释我的解释) GNU-g++ 4.8.3 和 clang 3.4.2

 # include <iostream>

 enum any : int; // unscoped opaque declaration :int required by the standard

 enum : int  {a} t; // unscoped anonymous declaration of t (:int not required)

 enum any : int {b} u; // redlecaration of type "any" with one enumerator

 enum class foo : char; // scoped opaque declaration "foo" required, :char NOT

 enum class foo : char {a, b} Foo; // redeclaration of "foo" with 2 
                              // enumerators. now :char REQUIRED


 enum class : char {d} Enum; // scoped anonymous declaration of Enum
                        // wouldn't be disallowed?

int main()
  {
   t = a; // assignment to "t"
   u = b; // assignment to "u"
   Foo = foo::a; // assignment to "Foo"
   Enum = decltype(Enum)::d;  // allowed (??)
  std::cout << static_cast<int>(t) << ' '
  << static_cast<int>(u) << ' '
  << static_cast<int>(Foo) << ' '
  << static_cast<int>(Enum) << std::endl;
  }

clang 拒绝该代码并在 Enum 声明中发出一条消息错误,指出“作用域枚举需要一个名称”; GNU-g++ 然而接受 它并执行在标准输出上放置四个零(正如预期的那样,一旦代码运行)。

请注意,当枚举器的名称“d”是 更改为“a”,就好像在这种情况下,错误声明的 Enum 会 是名称“a”与同名冲突的无作用域枚举 在“任何”类型中(至少这是我在阅读 诊断)。相反,GNU-g++ 也会(连贯地)接受名称“a” 为 Enum 的枚举数。

那么真相是什么?

最佳答案

这里的标准很明确。代码格式错误。

GNU-g++ however accepts it and executes putting four zero's on standard output (as would be expected, once the code is run).

这是 GCC bug 54216 .此错误已在 GCC 4.9 中修复,rejects您的代码符合预期。

Note that clang issues further errors when the enumerator's name "d" is changed to "a", as if, in that case, the erroneously declared Enum would be an unscoped enumeration with the name "a" conflicting with the same name in the type "any" (at least this is what I have interpreted reading the diagnostics).

Clang 可能假定您确实打算声明一个匿名无作用域 枚举,并根据该假设继续解析代码的其余部分。

关于c++ - 不透明和匿名枚举声明如何符合标准要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367564/

相关文章:

c++ - std::mem_fun 与 std::mem_fn

c++ - 消息分配运算符不使用交换

c++ - 打印 std::this_thread::get_id() 给出 "thread::id of a non-executing thread"?

c# - 枚举的自定义属性是否危险?

java - 如何在java中遍历枚举

c++ - 条件类型定义

c++ - 如何从 QML 更改 C++ 模型?

c++ - 影响C++中初始化的顺序

c++ - boost::heap::arity,它是什么?

java - 使用枚举值来表示二元运算符或函数