enum class
enum class is a scoped enumeration. Its enumerators live inside the type's name (Color::Red), do not convert silently to int, and can pick an explicit underlying integer type.
Detailed Explanation
C's enum leaked names. enum Color { Red, Green }; put Red in the surrounding scope, so a later enum Alert { Red }; was a redefinition. The enumerators also converted to int without a cast, so Red + 1 compiled and if (color) compiled. That is convenient in bit flags and disastrous in APIs: a function that wanted a Color would accept 42.
enum class (C++11, also spelled enum struct) closes both holes. The enumerators are members of the enumeration type. You write Color::Red. There is no implicit conversion to or from an integer. Color c = Color::Red; is fine. int n = Color::Red; is not; you need static_cast<int>(Color::Red). A switch on the enum is exhaustive-friendly: compilers warn when a case is missing if you do not write default.
The underlying type defaults to int and can be set: enum class Port : std::uint16_t { Http = 80, Https = 443 };. That matters when you serialise the value or put it in a packed file format. For a plain enum (unscoped), the underlying type is implementation-defined unless you specify it.
Unscoped enums are still legal. Use them when you need the implicit conversions on purpose, or when you are matching a C API. Even then you can write enum Color : unsigned { Red = 1 }; to pin the width. New code should start with enum class.
Enumerators can have explicit values. If you omit a value, it is the previous plus one, starting at 0. Gaps are allowed. Two names can share a value. Sequential values from 0 are the ones that switch jump tables like.
enum class does not have methods. If you need to_string(Color) or Color from_string(std::string_view), write free functions next to the type, or a small class that wraps the enum. C++23's std::to_underlying is the portable way to get the integer without repeating the underlying type.
Bitwise flags are a common pattern: enum class Perm : unsigned { Read = 1, Write = 2, Exec = 4 }; plus overloaded | and &. Without those overloads, Perm::Read | Perm::Write does not compile, because there is no implicit conversion to unsigned. That is the type system doing its job. You write the operators once.
Comparing two different enum classes does not compile. Comparing an enum class to an integer does not compile. Those are the bugs the feature exists to stop.
Code Examples
#include <iostream>
enum class Status { Ok, NotFound, Error };
const char* label(Status s) {
switch (s) {
case Status::Ok: return "ok";
case Status::NotFound: return "missing";
case Status::Error: return "error";
}
return "unknown";
}
int main() {
Status s = Status::NotFound;
std::cout << label(s) << '\n'; // prints: missing
std::cout << static_cast<int>(s) << '\n'; // prints: 1
// int n = s; // error: no implicit conversion
// Status t = 0; // error
}#include <cstdint>
#include <iostream>
enum class Perm : unsigned {
Read = 1u << 0,
Write = 1u << 1,
Exec = 1u << 2
};
constexpr Perm operator|(Perm a, Perm b) {
return static_cast<Perm>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
}
constexpr bool has(Perm set, Perm bit) {
return (static_cast<unsigned>(set) & static_cast<unsigned>(bit)) != 0;
}
int main() {
Perm p = Perm::Read | Perm::Write;
std::cout << std::boolalpha << has(p, Perm::Write) << '\n'; // prints: true
}Common Gotchas
Forward-declaring needs the underlying type
`enum class Color;` is enough to name the type. `enum Color;` (unscoped, no underlying type) is not enough to use it as a member; the compiler does not know the size.
`enum class` is not a namespace for functions
`Color::to_string` is not a thing unless you put a nested function in a wrapping class. Write `to_string(Color)` in the same namespace so ADL finds it.
`default` hides missing cases
when you add `Status::Timeout`, a `switch` with `default` stays silent. Omit `default` if the enum is closed.
Using the enumerator name without the type
`Red` is not in scope. Write `Color::Red`. A `using enum Color;` (C++20) pulls the names in for a local block.