C++11 在C++;11个作用域枚举?

C++11 在C++;11个作用域枚举?,c++11,gcc,enums,clang,language-lawyer,C++11,Gcc,Enums,Clang,Language Lawyer,以下代码与GCC 5.2.1一致 enum class some_enum { first = static_cast<some_enum>(1), }; 因此,由于这是一个没有显式枚举基的作用域枚举,因此其底层类型固定为int Following the closing brace of an enum-specifier, each enumerator has the type of its enumeration. ...and the constant-expr

以下代码与GCC 5.2.1一致

enum class some_enum
{
    first = static_cast<some_enum>(1),
};
因此,由于这是一个没有显式枚举基的作用域枚举,因此其底层类型固定为
int

Following the closing brace of an enum-specifier, each enumerator
has the type of its enumeration.
...and the constant-expression in the enumerator-definition shall
be a converted constant expression of the underlying type (5.19);
if the initializing value of an enumerator cannot be represented
by the underlying type, the program is ill-formed.
换句话说,在
枚举
主体之外,单个枚举数(即枚举中的命名条目)的类型与整个
枚举
的类型相同

If the underlying type is fixed, the type of each enumerator prior to
the closing brace is the underlying type...
由于我们处理的是一个始终具有固定基础类型的作用域枚举,因此枚举体中每个枚举项的类型都与枚举的基础类型具有相同的类型。在本例中,在主体中,枚举数的类型为
int

Following the closing brace of an enum-specifier, each enumerator
has the type of its enumeration.
...and the constant-expression in the enumerator-definition shall
be a converted constant expression of the underlying type (5.19);
if the initializing value of an enumerator cannot be represented
by the underlying type, the program is ill-formed.

在本例中,初始值设定项具有type
some_enum
,作为C++11作用域的枚举,如果不进行显式转换,则无法将其转换为基础类型的常量表达式。因此,该程序的格式不正确。

该段所指的是不同的枚举。例如,
enum X{a,b,c}枚举Y{x=a,Y=b,z=c}<代码>。这里,
Y
定义中的
a`类型是
X
的基本类型。gcc 5.2中没有第一个代码段。还有什么与错误消息中的第二个相同?@Praetorian抱歉,这是我的实验导致的草率复制+粘贴错误。我现在已经更正了代码段和错误。@BobbyMoretti:是否有任何东西表明
some_enum
在定义完成之前是一个完整的类型?我的假设是它不是,而且“注入的类名”在
enum类定义中不是一个东西。我的期望实际上是两个编译器都会因为这个原因拒绝您的代码。这不仅仅是迂腐,在我们知道声明了多少枚举数之前,枚举的基本类型可能是未知的,因此这可能会给编译器带来极大的困难。@BobbyMoretti:这可能只是未定义的行为,iiuc如果在标准模板中使用不完整的类型,通常会出现未定义的行为。(有时也会发生一件事,没有正确定义的符号会被编译器视为
int
,因此我并不认为编译的事实特别引人注目)编辑:嗯,但我确实看到你使用了“-pedantic”。。。不确定