c++ - GCC 拒绝使用 enum-base 的简单声明; clang 接受它——这是正确的吗?

标签 c++ c++11 enums language-lawyer c++14

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/

相关文章:

c++ - 意外的迭代器行为

c++ - 在不需要时使用 "template"和 "typename"消歧器

c++ - Visual Studio C++ 枚举需要很长时间才能编译

java - 为什么 Enum 中的静态方法 values() 和 valueOf() 是编译器添加的,而没有在 Enum 类中显式声明?

c++ - 为什么带有 lcov 的 codecov 在 Travis 上无法正常工作,但在我的本地 Linux Mint 上却无法正常工作?

c++ - 我从哪里获得适用于 VS21015 的正确 gdi+ c++ 包装器?

c++ - 如果我不知道每个列表中有多少个数字,如何将给定数量的数字列表作为输入?

c++ - Freetype 字形在加载到 openGL 时换行

c++ - 对 constexpr 函数有点困惑

java - JButton 的 Instanceof、Enum 或多个监听器