【问题标题】:How do opaque and anonymous enum declarations agree with the standard's requirements?不透明和匿名枚举声明如何符合标准的要求?
【发布时间】:2015-04-06 16:49:44
【问题描述】:

我在 N3936(第 7.2.2 条)中读到“在范围枚举的声明中不应省略可选标识符”,所以我尝试了以下代码 (嵌入的 cmets 试图解释我的解释) 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 的枚举器。

那么真相是什么?

【问题讨论】:

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


    【解决方案1】:

    这里的标准很清楚。代码格式不正确。

    GNU-g++ 但是接受它并执行在标准输出上放置四个零(正如预期的那样,一旦代码运行)。

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

    请注意,当枚举器的名称为“d”时,clang 会发出更多错误 更改为“a”,就好像在这种情况下,错误声明的 Enum 将是名称“a”的无范围枚举与 “any”类型中的相同名称(至少这是我所拥有的 解释读取诊断)。

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

    【讨论】:

    • 谢谢。我没有找到那个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2018-10-22
    相关资源
    最近更新 更多