2015-02-06 39 views
3

我在N3936(7.2.2条款)中读到“在范围枚举的声明中不应该省略可选标识符”,所以我尝试了下列代码 (其中嵌入的注释试图解释我的解释)与 GNU-G ++ 4.8.3和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; 
    } 

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

注意铛问题进行进一步的错误时,枚举的名称为“d”是 改为“一”,那样的话,在这种情况下,错误地宣布枚举将 是未范围枚举名称为“”冲突与类型为“any”的同名 (至少这是我解释的读取 诊断)。相反,GNU-g ++也会接受(连贯地)Enum枚举器的名称“a” 。

那么,真相是什么?

回答

3

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

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

这是GCC bug 54216。这个bug已经在GCC 4.9中得到修复,其中rejects代码如预期的那样。

注意当枚举的名称为“d” 更改为“一”,那样的话,在这种情况下,该铛问题进行进一步的错误,错误地宣布枚举 将是一个无范围的列举名称为“A”与 冲突的类型为“任何”(至少这是我有 解释读取诊断)相同的名称。

锵大概都会认为你的真正用意声明匿名未范围枚举,并着手解析这个假设的代码的其余部分。

+0

谢谢。我没有发现这个错误。 – GSi 2015-02-09 09:28:37