Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strong enum typedef: clang bug or c++11 standard uncertainty?

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++.

like image 486
user1244932 Avatar asked Oct 24 '18 14:10

user1244932


1 Answers

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.

like image 118
Oliv Avatar answered Nov 12 '22 00:11

Oliv