38

Given the program:

enum E : int
{
    A, B, C
};

g++ -c test.cpp works just fine. However, clang++ -c test.cpp gives the following errors:

test.cpp:1:6: error: ISO C++ forbids forward references to 'enum' types
enum E : int
     ^
test.cpp:1:8: error: expected unqualified-id
enum E : int
       ^
2 errors generated.

These error messages don't make any sense to me. I don't see any forward references here.

7
  • FWIW, GCC now (as of 5 or 6) compiles with ‑std=c++14 as the default, while Clang still uses ‑std=c++98 AFAIK.
    – chris
    Commented Aug 4, 2016 at 21:45
  • 2
    I think it would help if you included compiler versions and options. Commented Aug 4, 2016 at 21:45
  • 7
    This seems like a reasonable question to me, it is not a typographical error, and it is not a question seeking debugging help that doesn't include the code and error message. Commented Aug 4, 2016 at 21:59
  • 3
    didn't vote, but using -std=c++11 is the first thing to try when you use a C++11 feature and get strange error messages
    – M.M
    Commented Aug 4, 2016 at 23:36
  • 4
    Didn't know at the time that it was a C++11 feature. The syntax is not new - the MS compilers have supported this syntax since at least VS2005. Commented Aug 4, 2016 at 23:37

1 Answer 1

36

Specifying the underlying type for an enum is a C++11 language feature. To get the code to compile, you must add the switch -std=c++11. This works for both GCC and Clang.

For enums in C++03, the underlying integral type is implementation-defined, unless the values of the enumerator cannot fit in an int or unsigned int. (However, Microsoft's compiler has allowed specifying the underlying type of an enum as a proprietary extension since VS 2005.)

2
  • 2
    You're right: if you compile with option -std=c++11 is works (online demo). With gcc older than 6 give you an explicit warning. With gcc 6 no nead for the std flag.
    – Christophe
    Commented Aug 4, 2016 at 22:05
  • 2
    @Christophe: Which is to say: with gcc 6, they (finally) changed it to compile C++11 by default, and require a switch to conform with older standards. Commented Aug 4, 2016 at 23:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.