Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

GCC 4.9.2 doesn't compile this snippet, but clang 3.5.0 does. Which one is correct?

enum F : int { x, y, z};
int F;
enum F:int f = F::x;

GCC output :

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;
                ^

I believe GCC is correct, as a simple-declaration (containing the elaborated-type-specifier enum F) doesn't allow an enum-base (: int), but I'd like some confirmation on this.

like image 577
Belloc Avatar asked Apr 24 '15 14:04

Belloc


2 Answers

I believe gcc is correct. If we look at the grammar rules in [dcl.enum], the type specifier comes in with:

enum-base:
: type-specifier-seq

The tokens that contain an enum-base are:

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

and

opaque-enum-declaration:
   enum-key attribute-specifier-seqopt identifier enum-baseopt;

This expression:

enum F:int f = F::x; 

Is neither an enum-specifier (no {}s present) nor an opaque-enum-declaration (in which the type-specifier would be followed immediately by a ;). Since it's not in the C++ grammar, it's not a valid expression.

like image 158
Barry Avatar answered Oct 21 '22 01:10

Barry


Your reasoning is correct. An enum-base like ": int" is syntactically allowed only in an enum-specifier, which must contain a { bracketed } list of enumerators, or in an opaque-enum-declaration, which must follow the enum-base with an immediate semicolon ;.

like image 38
aschepler Avatar answered Oct 21 '22 00:10

aschepler