【问题标题】:GCC rejects a simple-declaration with an enum-base; clang accepts it — which is correct?GCC 拒绝使用 enum-base 的简单声明; clang 接受它——这是正确的吗?
【发布时间】:2015-07-03 04:30:17
【问题描述】:

GCC 4.9.2 不会编译这个 sn-p,但 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(@ 987654326@),但我想确认一下。

【问题讨论】:

  • 最没有帮助的问题标题:-S
  • 我可以肯定地说F 不是enum class 所以F::x 是无效的。
  • @GreenScape,你不需要一个作用域枚举来使用作用域解析运算符。
  • @KerrekSB 写评论的时间本可以花在修复标题上。
  • @PeterSchneider:你无法修复整个世界 :-( ......至少不是每天都这样!(但抱怨它很便宜。)

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


【解决方案1】:

我相信 gcc 是正确的。如果我们看一下 [dcl.enum] 中的语法规则,类型说明符带有:

枚举基数:
: type-specifier-seq

包含 enum-base 的标记是:

枚举说明符:
enum-head { enumerator-listopt }
枚举头 { 枚举器列表 , }
枚举头:
enum-key attribute-specifier-seqopt identifieropt enum-baseopt
enum-key attribute-specifier-seqopt nested-name-specifier 标识符 枚举-baseopt

不透明枚举声明:
enum-key attribute-specifier-seqopt identifier enum-baseopt;

这个表达式:

enum F:int f = F::x;

既不是 enum-specifier(不存在 {}s)也不是 opaque-enum-declaration(其中类型说明符将紧跟;)。因为它不在 C++ 语法中,所以它不是一个有效的表达式。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2021-06-11
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多