C++ Reference ยท Keywords and Statements
The enum and enum class keywords
Reference for C++ enum and enum class: scoped vs unscoped enumerations, underlying types, conversions, using enum, std::to_underlying and flag patterns.
An enumeration is a type whose values are a set of named constants. C++ has two kinds. A scoped enumeration, enum class, keeps its enumerators inside its own scope and never converts implicitly to an integer. An unscoped enumeration, plain enum, is inherited from C: its enumerators leak into the surrounding scope and convert to int automatically.
Prefer enum class in new code. It prevents name clashes, forbids accidental arithmetic with unrelated enums, and lets you forward-declare the type because its underlying type is known (int unless you pick another).
enum class Color { Red, Green, Blue }; Color c = Color::Green;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
enum Name { A, B = 5, C }; // unscoped enum class Name : underlying-type { A, B }; // scoped (struct also allowed) enum Name : underlying-type; // opaque declaration using enum Name; // C++20
| Part | Description |
|---|---|
Name |
Optional for unscoped enums (anonymous enumerations are allowed). |
underlying-type |
An integral type. For enum class the default is int. For an unscoped enum without a fixed type the compiler picks a type big enough for all values. |
enumerator = value |
A constant expression. Without it an enumerator is the previous value plus one (the first is 0). Duplicate values are allowed. |
Usage notes
enum structis a synonym forenum class.- Scoped enumerators need the prefix:
Color::Red. Unscoped ones can be used asRed, and polluting the surrounding scope is the main reason to avoid them. - Arithmetic and bitwise operators are not defined for scoped enums; define the ones you need (as in the flags example) or use
std::to_underlying(C++23,<utility>) /std::underlying_type_t. - Converting an integer outside the range of the enumeration's values to an enum with a fixed underlying type is fine; for enums without a fixed type it is undefined behavior (before C++17, unspecified).
static_castdoes not validate. - Scoped enums support comparison operators with the same enum type, including
<=>in C++20. - C++20
using enumimports all enumerators and works for both kinds, including in aswitch. - There is no built-in way to get an enumerator's name or to iterate. Write a
switchor table, or use a library; C++26 reflection will address it. - Enums with a fixed underlying type can be forward-declared in headers:
enum class Color : int;. - Mixing enums and integers silently is the problem scoped enums solve; do not add
operator intto bring it back.