enum
Type Declarations ¶The C23 and C++11 standards added new syntax to specify the underlying
type of an enum
type. For example,
enum pet : unsigned char { CAT, DOG, ROCK };
In GCC, this feature is supported as an extension in all older dialects of C and C++ as well. For C++ dialects before C++11, use -Wno-c++11-extensions to silence the associated warnings.
You can also forward-declare an enum
type, without specifying
its possible values. The enumerators are supplied in a later
redeclaration of the type, which must match the underlying type of the
first declaration.
enum pet : unsigned char; static enum pet my_pet; ... enum pet : unsigned char { CAT, DOG, ROCK };
Forward declaration of enum
types with an explicit underlying
type is also a feature of C++11 that is supported as an extension by
GCC for all C dialects. However, it’s not available in C++ dialects
prior to C++11.
The C++ standard refers to a forward declaration of an enum
with an explicit underlying type as an opaque type. It is not
considered an incomplete type, since its size is known. That means
you can declare variables or allocate storage using the type before
the redeclaration, not just use pointers of that type.
GCC has also traditionally supported forward declarations of
enum
types that don’t include an explicit underlying type
specification. This results in an incomplete type, much like what you
get if you write struct foo
without describing the elements.
You cannot allocate variables or storage using the type while it is
incomplete. However, you can work with pointers to that type.
Forward-declaring an incomplete enum type without an explicit underlying type is supported as an extension in all GNU C dialects, but is not supported at all in GNU C++.