GCC 拒绝带有枚举基础的简单声明; clang 接受它——哪个是正确的?

GCC rejects a simple-declaration with an enum-base; clang accepts it — which is correct?

GCC 4.9.2 doesn't compile this snippet, but 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 是正确的,因为 简单声明 (包含详尽的类型说明符 enum F)不允许 enum-base (: int),但我想对此进行一些确认。

你的推理是正确的。像“: int”这样的 enum-base 在句法上只允许在 enum-specifier 中使用,它必须包含 { 括起来的 } 枚举数列表,或在 opaque-enum-declaration 中,它必须紧跟在 enum-base 之后分号 ;.

我相信 gcc 是正确的。如果我们查看 [[=​​39=]] 中的语法规则,类型说明符包含:

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(在其中类型说明符将紧跟 ;)。由于它不在 C++ 语法中,因此它不是有效的表达式。