Syntax
*enum-specifier:*
:: **enum** attribute-specifier-sequenceopt identifieropt { *enumerator-list* **}**
:: **enum** attribute-specifier-sequenceopt identifieropt { *enumerator-list* , **}**
:: **enum** *identifier*
*enumerator-list:*
:: *enumerator*
:: *enumerator-list* **,** *enumerator*
*enumerator:*
:: *enumeration-constant* attribute-specifier-sequenceopt
:: *enumeration-constant* attribute-specifier-sequenceopt **=** *constant-expression*
Constraints
The expression that defines the value of an enumeration constant shall be an integer constant expression. that has a value representable as an `int`. For all the integer constant expressions which make up the values of the enumeration constant, there shall be an implementation-defineda signed or unsigned integer type (excluding the bit-precise integer types) capable of representing all of the values.
Semantics
The identifiers in an enumerator list are declared as constants and may appear wherever such are permitted. An enumerator with **=** defines its enumeration constant as the value of the constant expression. If the first enumerator has no **=**, the value of its enumeration constant is 0. Each subsequent enumerator with no **=** defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with **=** may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.
The type for the members of an enumeration is called the *enumeration member types**enumeration member type*.
During the processing of each enumeration constant in the enumerator list, the type of the enumeration constant shall be:
:: β `int`, if there are no previous enumeration constants in the enumerator list and no explicit **=** with a defining integer constant expression; or,
:: β `int`, if given explicitly with **=** and the value of the integer constant expression is representable by an `int`; or,
:: β the type of the integer constant expression, if given explicitly with **=** and if the value of the integer constant expression is not representable by `int`; or,
:: β the type of the value from lastthe previous enumeration constant with `1` added to it. If such an integer constant expression would overflow or wraparound the value of the previous enumeration constant from the addition of `1`, the type takes on either:
:: :: β a suitably sized signed integer type (excluding the bit-precise signed integer types) capable of representing the value of the previous enumeration constant plus `1`; or,
:: :: β a suitably sized unsigned integer type (excluding the bit-precise unsigned integer types) capable of representing the value of the previous enumeration constant plus `1`.
:: A signed integer type is chosen if the previous enumeration constant being added is of signed integer type. An unsigned integer type is chosen if the previous enumeration constant is of unsigned integer type. If there is no suitably sized integer type described previous which can represent the new value, then the enumeration has no type which is capable of representing all of its values.
Each enumerated type shall be compatible with `char`, a signed integer type, or an unsigned integer type (excluding the bit-precise integer types). The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.
The enumerated type is incomplete until immediately after the **}** that terminates the list of enumerator declarations, and complete thereafter. The enumeration member type upon completion is:
:: β `int` if all the values of the enumeration are representable as an `int`; or,
:: β the enumerated type
**EXAMPLE** The following fragment:
```cpp
enum hue { chartreuse, burgundy, claret=20, winedark };
enum hue col, *cp;
col = claret;
cp = &col;
if (*cp != burgundy)
/* ... */
```
makes hue the tag of an enumeration, and then declares col as an object that has that type and `cp` as a pointer to an object that has that type. The enumerated values are in the set {0, 1, 20, 21}.
### Add implementation-defined enumeration behavior to Annex J ### {#wording-specification-annex-j}
# Acknowledgements # {#acknowledgements}
Thanks to:
- Aaron Ballman for help with the initial drafting;
- Aaron Ballman, Aaron Bachmann, Jens Gustedt & Joseph Myers for questions, suggestions and offline discussion;
- Joseph Myers, for detailed review of the many components and fine-tuning of the way enumeration constants are handled;
- Jens Gustedt, for detailed review and suggestions helping to shape this paper;
- Robert Seacord for editing suggestions; and,
- Clive Pygott for the initial draft of this paper.
We hope this paper serves you all well.