【发布时间】: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