An enum in C++ defines a set of named integer constants, and an enum class (scoped enumeration, since C++11) does the same but keeps the names inside the enum's own scope and stops silent conversion to int. For new code, use enum class by default and use a plain enum only when you need implicit conversion to integers or must match legacy C code.
This guide compares both forms, shows how to choose the underlying type, how to print and convert values safely, and how to use enum class with switch. Examples compile with g++ -std=c++20.
A plain (unscoped) enumeration creates named constants. By default the first is 0 and each next name is one more than the previous.
Plain enums have two weaknesses. Their names leak into the surrounding scope, so Red is now a global name and two enums cannot both have an enumerator called Red. And they convert to int automatically, so nonsense such as Red == 0, Red + Blue or comparing two unrelated enums compiles without complaint.
An enum class (you can also write enum struct, which means the same) puts its enumerators in the scope of the enum and has no implicit conversion to integers.
You must always write Color::Green, which makes the code self-documenting and removes name clashes. The type checks catch mixing a Color with a Light or with a plain number at compile time.
You can choose the integer type that stores the enum and assign explicit values. A scoped enum uses int unless you say otherwise.
A fixed underlying type matters for file formats, network protocols and memory layout. It also lets you forward-declare a scoped enum (enum class Status : std::uint8_t;) to reduce header dependencies. Plain enums can do the same only when you also give the underlying type.
In C++23, std::to_underlying(value) from <utility> converts an enum to its underlying type without naming it, which is safer than a static_cast<int>.
An enum class works well with switch. Leave out default so the compiler can warn when a new enumerator is not handled (-Wswitch, enabled by -Wall).
An enum class does not support bitwise operators by default. For flag sets, define the operators yourself.
Neither kind of enum can be printed by name automatically. std::cout << Color::Red does not compile for a scoped enum, so write a small function like name() above or cast to the underlying type.
| Feature | enum | enum class |
|---|---|---|
| Names in enclosing scope | yes | no, use Type::Name |
| Implicit conversion to int | yes | no, needs a cast |
| Default underlying type | implementation chosen | int |
| Forward declaration | only with fixed type | yes |
| Bitwise operators | work through int | need your own operators |
Use enum class for new code. Choose plain enum when you need the constants to convert to integers without casts, for example array indexes in tight, contained code, or when interfacing with a C header. Another option for compile-time constants without a type is constexpr variables.
In new code, yes, unless you specifically need implicit integer conversion. The scoping and type safety prevent real bugs at no runtime cost.
Use static_cast<int>(value), or static_cast<std::underlying_type_t<E>>(value) to be exact. In C++23 you can call std::to_underlying(value).
Not directly, because C++ has no enumeration of enumerators. Keep an array of the values or add a final Count enumerator and loop over the integers up to it.
If the enum has a fixed underlying type, the result is a valid but unnamed value. Without one, an out-of-range value is undefined behaviour in C++17 and later, so validate input before casting.
enum creates integer constants in the surrounding scope that convert implicitly to int, while enum class scopes the names and blocks implicit conversion. Prefer enum class, give it a fixed underlying type when layout matters, cast explicitly when you need the number, and let switch warnings tell you about unhandled values.