enum
suggest change- Introduces the definition of an enumeration type.
enum Direction { UP, LEFT, DOWN, RIGHT }; Direction d = UP;
In C++11,
enum
may optionally be followed byclass
orstruct
to define a scoped enum. Furthermore, both scoped and non-scoped enums can have their underlying type explicitly specified by: T
following the enum name, whereT
refers to an integer type.enum class Format : char { TEXT, PDF, OTHER }; Format f = Format::TEXT; enum Language : int { ENGLISH, FRENCH, OTHER };
Enumerators in normal
enum
s may also be preceded by the scope operator, although they are still considered to be in the scope theenum
was defined in.Language l1, l2; l1 = ENGLISH; l2 = Language::OTHER;
- Introduces an elaborated type specifier, which specifies that the following name is the name of a previously declared enum type. (An elaborated type specifier cannot be used to forward-declare an enum type.) An enum can be named in this way even if hidden by another name.
enum Foo { FOO }; void Foo() {} Foo foo = FOO; // ill-formed; Foo refers to the function enum Foo foo = FOO; // ok; Foo refers to the enum type
- Introduces an opaque enum declaration, which declares an enum without defining it. It can either redeclare a previously declared enum, or forward-declare an enum that has not been previously declared.
An enum first declared as scoped cannot later be declared as unscoped, or vice versa. All declarations of an enum must agree in underlying type.
When forward-declaring an unscoped enum, the underlying type must be explicitly specified, since it cannot be inferred until the values of the enumerators are known.
enum class Format; // underlying type is implicitly int void f(Format f); enum class Format { TEXT, PDF, OTHER, }; enum Direction; // ill-formed; must specify underlying type
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents