C++ Enums
In this page:
What Is an Enum?
An enum defines a type whose values are restricted to a small named set, like Color { RED, GREEN, BLUE }, making code that models a fixed set of options far more readable than using raw integers with unclear meaning. Under the hood each enumerator is stored as an integer, starting at 0 by default and incrementing for each following name.
Example: What Is an Enum?
#include <iostream>
enum Color { RED, GREEN, BLUE };
int main() {
Color c = GREEN;
std::cout << c << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Plain Enums and Their Pitfalls
A plain enum implicitly converts to int and its enumerator names leak into the surrounding scope, which means two plain enums in the same scope can't reuse a name like RED without a naming collision. This looseness also lets you accidentally compare or assign values from unrelated enums, since the compiler doesn't stop the implicit int conversion.
Example: Plain Enums and Their Pitfalls
#include <iostream>
enum Color { RED, GREEN, BLUE };
int main() {
int x = RED; // implicitly converts to int
std::cout << x << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Enum Class for Type Safety
enum class (C++11+) scopes its enumerators inside the enum's own name (Color::RED) and does not implicitly convert to int, so mixing up two different enum types becomes a compile error instead of a silent bug. This is why modern C++ style strongly prefers enum class over the plain, unscoped enum in new code.
Example: Enum Class for Type Safety
#include <iostream>
enum class Color { RED, GREEN, BLUE };
int main() {
Color c = Color::RED; // scoped, no implicit int conversion
std::cout << (c == Color::RED) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Setting the Underlying Type
By default an enum's underlying integer type is implementation-defined, but you can pin it explicitly, e.g. enum class Status : uint8_t, to control the type's storage size, which matters when the enum is part of a struct laid out for a specific memory footprint or serialized format.
Example: Setting the Underlying Type
#include <iostream>
#include <cstdint>
enum class Status : uint8_t { Active, Inactive }; // explicitly pinned type
int main() {
Status s = Status::Active;
std::cout << static_cast<int>(s) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Enums in Switch Statements
Enums pair naturally with switch statements, since the small closed set of values makes it easy for both the programmer and the compiler to reason about every possible case; many compilers will even warn you if a switch on an enum class doesn't handle every enumerator.
Example: Enums in Switch Statements
#include <iostream>
enum class Color { RED, GREEN, BLUE };
int main() {
Color c = Color::GREEN;
switch (c) {
case Color::RED: std::cout << "Red" << std::endl; break;
case Color::GREEN: std::cout << "Green" << std::endl; break;
case Color::BLUE: std::cout << "Blue" << std::endl; break;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: