For such code:
typedef enum FooEnum : int FooEnum;
enum FooEnum : int { A = 1, B };
clang (linux/7.0.0) reports no errors [-c -std=c++11 -pedantic
],
but gcc (linux/8.2.1) doesn't compile it:
g++ -c -std=c++11 -pedantic test2.cpp
test2.cpp:1:28: error: expected ';' or '{' before 'FooEnum'
typedef enum FooEnum : int FooEnum;
^~~~~~~
test2.cpp:1:28: error: expected class-key before 'FooEnum'
test2.cpp:2:16: error: using typedef-name 'FooEnum' after 'enum'
enum FooEnum : int { A = 1, B };
^~~
test2.cpp:1:28: note: 'FooEnum' has a previous declaration here
typedef enum FooEnum : int FooEnum;
In fact I have no idea why use typedef for enum in C++, but question is this is bug in clang, because it accepts invalid code, or this is bug in c++11 standard, that allow different implementation?
Update: as it was explained to me, the first typedef is used for objc++ compability, to use the same header during c++ code compilation and objc++.
This is a clang bug, you cannot have a opaque-enum-declaration after a typedef
specifier.
[dcl.typedef]/1
The typedef specifier shall not be combined in a decl-specifier-seq with any other kind of specifier except a defining-type-specifier,[...]
[dcl.type]/1
defining-type-specifier:
type-specifier
class-specifier
enum-specifier
[dcl.enum]/1
enum-specifier:
enum-head { enumerator-listopt }
enum-head { enumerator-list , }
So the code bellow is legal c++:
typedef enum FooEnum : int { A = 1, B } FooEnum;
but this one is not legal c++:
typedef enum FooEnum : int FooEnum;
Because enum FooEnum:int
is not a defining-type-specifier.
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