[dcl.enum] (original) (raw)
9 Declarations [dcl.dcl]
9.7 Enumerations [enum]
9.7.1 Enumeration declarations [dcl.enum]
The enumeration type declared with an enum-keyof only enum is an unscoped enumeration, and its enumerators are unscoped enumerators.
The enum-keys enum class andenum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators.
The optional enum-head-name shall not be omitted in the declaration of a scoped enumeration.
The type-specifier-seq of an enum-baseshall name an integral type; any cv-qualification is ignored.
An opaque-enum-declaration declaring an unscoped enumeration shall not omit the enum-base.
The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required.
If the first enumeratorhas no initializer, the value of the corresponding constant is zero.
An enumerator-definition without aninitializer gives the enumerator the value obtained by increasing the value of the previous enumeratorby one.
[ Example
:
enum { a, b, c=0 }; enum { d, e, f=e+2 };
defines a, c, and d to be zero, b ande to be 1, and f to be 3.
— end example
]
An opaque-enum-declaration is either a redeclaration of an enumeration in the current scope or a declaration of a new enumeration.
[ Note
:
An enumeration declared by anopaque-enum-declaration has a fixed underlying type and is a complete type.
The list of enumerators can be provided in a later redeclaration with an enum-specifier.
— end note
]
A scoped enumeration shall not be later redeclared as unscoped or with a different underlying type.
An unscoped enumeration shall not be later redeclared as scoped and each redeclaration shall include an enum-base specifying the same underlying type as in the original declaration.
Each enumeration defines a type that is different from all other types.
Each enumeration also has an underlying type.
The underlying type can be explicitly specified using an enum-base.
For a scoped enumeration type, the underlying type is int if it is not explicitly specified.
In both of these cases, the underlying type is said to befixed.
Following the closing brace of an enum-specifier, each enumerator has the type of its enumeration.
If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type and the constant-expression in the enumerator-definitionshall be aconverted constant expressionof the underlying type.
If the underlying type is not fixed, the type of each enumerator prior to the closing brace is determined as follows:
- If the expression has unscoped enumeration type, the enumerator has the underlying type of that enumeration type, otherwise it has the same type as the expression.
- If no initializer is specified for the first enumerator, its type is an unspecified signed integral type.
- Otherwise the type of the enumerator is the same as that of the preceding enumerator unless the incremented value is not representable in that type, in which case the type is an unspecified integral type sufficient to contain the incremented value.
If no such type exists, the program is ill-formed.
An enumeration whose underlying type is fixed is an incomplete type from itspoint of declaration to immediately after itsenum-base (if any), at which point it becomes a complete type.
An enumeration whose underlying type is not fixed is an incomplete type from its point of declaration to immediately after the closing } of itsenum-specifier, at which point it becomes a complete type.
For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration.
If no integral type can represent all the enumerator values, the enumeration is ill-formed.
It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than intunless the value of an enumerator cannot fit in an int orunsigned int.
If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0.
For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type.
Otherwise, the values of the enumeration are the values representable by a hypothetical integer type with minimal width Msuch that all enumerators can be represented.
The width of the smallest bit-field large enough to hold all the values of the enumeration type is M.
It is possible to define an enumeration that has values not defined by any of its enumerators.
If the enumerator-list is empty, the values of the enumeration are as if the enumeration had a single enumerator with value 0.94
Two enumeration types are layout-compatible enumerationsif they have the same underlying type.
The value of an enumerator or an object of an unscoped enumeration type is converted to an integer by integral promotion.
[ Example
:
enum color { red, yellow, green=20, blue }; color col = red; color* cp = &col; if (*cp == blue)
makes color a type describing various colors, and then declarescol as an object of that type, and cp as a pointer to an object of that type.
The possible values of an object of typecolor are red, yellow, green,blue; these values can be converted to the integral values0, 1, 20, and 21.
Since enumerations are distinct types, objects of type color can be assigned only values of type color.
color c = 1;
int i = yellow;
Note that this implicit enum to intconversion is not provided for a scoped enumeration:
enum class Col { red, yellow, green };
int x = Col::red;
Col y = Col::red;
if (y) { }
— end example
]
Each enum-name and each unscoped enumerator is declared in the scope that immediately contains the enum-specifier.
Each scoped enumerator is declared in the scope of the enumeration.
An unnamed enumeration that does not have a typedef name for linkage purposes ([dcl.typedef]) and that has a first enumerator is denoted, for linkage purposes ([basic.link]), by its underlying type and its first enumerator; such an enumeration is said to have an enumerator as a name for linkage purposes.
[ Note
:
Each unnamed enumeration with no enumerators is a distinct type.
— end note
]
[ Example
:
enum direction { left='l', right='r' };
void g() {
direction d;
d = left;
d = direction::right;
}
enum class altitude { high='h', low='l' };
void h() {
altitude a;
a = high;
a = altitude::low;
}
— end example
]
An enumerator declared in class scope can be referred to using the class member access operators (::, . (dot) and ->(arrow)), see [expr.ref].
[ Example
:
struct X { enum direction { left='l', right='r' }; int f(int i) { return i==left ? 0 : i==right ? 1 : 2; } };
void g(X* p) {
direction d;
int i;
i = p->f(left);
i = p->f(X::right);
i = p->f(p->left);
}
— end example
]