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.
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.
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 ;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With