-
Enumeration types, as well as the contained enumerators, shall begin with an uppercase letter. Avoid writing the enumerators in all-uppercase notation.
Example:
enum class Tide { low, high };
Not:
enum class Tide { LOW, HIGH };
-
Use scoped enums (
enum class) instead of old-style enums (enum), since enum classes do not pollute the surrounding scope. Convert existing code usingenumto make use ofenum classinstead.-
Scoped enums are not visible from the surrounding scope, greatly reducing the possibility of name clashes.
-
Scoped enums do not implicitly convert to
int, whereas conventional enums do (which is often unwanted). Scoped enums can still be explicitly cast to an integer type. -
The underlying type of a scoped enum can be explicitly specified. This also enables forward declaration of scoped enums.
-
-
Consider providing an explicit type specification for an
enum class, in order to guarantee the size of the enumeration, and to enable forward declaration. By default, the underlying type isint.Example:
enum class Difficulty : unsigned char { easy, medium, hard }