Enum classes combine aspects of traditional enumerations (names values) with aspects of classes (scoped members and absence of conversions). Being able to specify the underlying type allow simpler interoperability and guaranteed sizes of enumerations and also enables forward declaration.
Enum classes address three problems with traditional C++ enumerations:
- Implicit int conversion.
- Enumerators exported to the surrounding scope.
- Their underlying type cannot be specified.
enum class
are strongly typed and scoped:
// traditional enum
enum Alert { green, yellow, orange, red };
// scoped and strongly typed enum
// no export of enumerator names into enclosing scope
enum class Color { red, blue };
// no implicit conversion to int
enum class TrafficLight { red, yellow, green };
Alert a = 7; // error (as ever in C++)
Color c = 7; // error: no int->Color conversion
int a2 = red; // ok: Alert->int conversion
int a3 = Alert::red; // error in C++98; ok in C++11
int a4 = blue; // error: blue not in scope
int a5 = Color::blue; // error: not Color->int conversion
Color a6 = Color::blue; // ok
underlying type can be specified
Being able to specify the underlying type allow simpler interoperability and guaranteed sizes of enumerations:
enum class Color : char { red, blue };
// by default, the underlying type is int
enum class TrafficLight { red, yellow, green };
// how big is an E? (whatever the rules say; i.e. "implementation defined")
enum E { E1 = 1, E2 = 2, Ebig = 0xFFFFFFF0U };
// now we can be specific
enum EE : unsigned long { EE1 = 1, EE2 = 2, EEbig = 0xFFFFFFF0U };
forward declaration enabled
enum class Color_code : char; // (forward) declaration
void foobar(Color_code* p); // use of forward declaration
enum class Color_code : char { red, yellow, green, blue }; // definition