GCC 4.9.2不编译此代码段,但 clang 3.5.0做。哪一个是正确的?
enum F : int { x, y, z};
int F;
enum F:int f = F::x;
GCC 输出:
main.cpp:3:12: error: expected ';' or '{' before 'f'
enum F:int f = F::x;
^
main.cpp:3:12: error: expected class-key before 'f'
main.cpp:3:14: error: invalid type in declaration before '=' token
enum F:int f = F::x;
^
main.cpp:3:16: error: 'F' is not a class, namespace, or enumeration
enum F:int f = F::x;
^
我相信 GCC 是正确的,因为 simple-declaration(包含详细类型说明符 enum F
)不允许 enum-base (: int
),但我想确认一下。
最佳答案
我相信 gcc 是正确的。如果我们看一下 [dcl.enum] 中的语法规则,类型说明符带有:
enum-base:
:
type-specifier-seq
包含 enum-base 的标记是:
enum-specifier:
enum-head{
enumerator-listopt}
enum-head{
enumerator-list,
}
enum-head:
enum-key attribute-specifier-seqopt identifieropt enum-baseopt
enum-key attribute-specifier-seqopt nested-name-specifier identifier enum-baseopt
和
opaque-enum-declaration:
enum-key attribute-specifier-seqopt identifier enum-baseopt;
这个表达式:
enum F:int f = F::x;
既不是 enum-specifier(不存在 {}
)也不是 opaque-enum-declaration(其中 type-说明符后紧跟 ;
)。因为它不在 C++ 语法中,所以它不是一个有效的表达式。
关于c++ - GCC 拒绝使用 enum-base 的简单声明; clang 接受它——这是正确的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849688/