Name

enum keyword — Declares enumerated type or elaborates an enumerated type name

Synopsis

               enum-specifier ::= enum [identifier] { [enumerator-list] }
enumerator-list ::= enumerator-defn | enumerator-list , enumerator-defn
               enumerator-defn ::= enumerator | enumerator = constant-expr
               enumerator ::= identifier
               elaborated-type-specifier := enum [::] [nested-name ::] identifier
            

The enum keyword declares a new enumerated type (as an enum-specifier) or names an existing enumerated type (in an elaborated-type-specifier). An enumerated type is an integral type that defines a set of named constants (the enumerator-list). Each enumerator is an identifier optionally followed by a value (an equal sign and a constant expression of integral or enumerated type). Without an explicit value, the value of an enumerator is one more than the value of the preceding enumerator. The implicit value of the first enumerator is 0.

image with no caption

Every enumerated type is stored as an integral type. The size of the integer is implementation-defined but is large enough to hold all the enumerated values. The valid values of an enumeration include the declared enumerators and every value that can be stored in the same number of bits, even if those values are not named as enumerators.

Example

enum logical { no, maybe, yes };
bool logical_to_bool(enum logical x) // Redundant enum
{
  return x != no;
}

See Also

expression, identifier, type ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.