指定枚举基础类型时的"ISO C++ forbids forward references to 'enum' types"

"ISO C++ forbids forward references to 'enum' types" when specifying enum underlying type

本文关键字:types forward references to enum forbids ISO 枚举 类型 C++      更新时间:2023-10-16

给定程序:

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

g++ -c test.cpp运行良好。然而,clang++ -c test.cpp给出以下错误:

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.

这些错误消息对我来说毫无意义。我在这里看不到任何转发参考。

为枚举指定底层类型是C++11语言的一项功能。要获得要编译的代码,必须添加开关-std=c++11。这对GCC和Clang都有效。

对于C++03中的枚举,除非枚举器的值不能容纳在int或无符号int中,否则底层整型是由实现定义的。(但是,自VS 2005以来,Microsoft的编译器允许将枚举的底层类型指定为专有扩展。(