C++ Enums
An enumeration (enum) is a type whose value is one of a small, named set of constants: the days of the week, the suits of a card deck, the states of a traffic light. Enums make code self-explanatory — Color::Red says far more than the magic number 0.
Scoped enums: enum class
Modern C++ code uses scoped enumerations, declared with enum class. Their values must be written with the type name in front:
#include <iostream>
using namespace std;
enum class Light { Red, Yellow, Green };
int main() {
Light current = Light::Red;
if (current == Light::Red) {
cout << "Stop!\n";
}
current = Light::Green;
cout << "Underlying value of Green: " << static_cast<int>(current) << "\n";
}Stop!
Underlying value of Green: 2
The values are numbered from 0 by default (Red = 0, Yellow = 1, Green = 2). A scoped enum does not convert to int automatically — you need static_cast. That strictness prevents accidental mistakes such as adding a colour to a number.
Enums and switch
Enums and switch are a perfect match. With -Wall, the compiler even warns you when a case is missing:
#include <iostream>
#include <string>
using namespace std;
enum class Light { Red, Yellow, Green };
Light next(Light l) {
switch (l) {
case Light::Red: return Light::Green;
case Light::Green: return Light::Yellow;
case Light::Yellow: return Light::Red;
}
return Light::Red;
}
string name(Light l) {
switch (l) {
case Light::Red: return "red";
case Light::Yellow: return "yellow";
case Light::Green: return "green";
}
return "?";
}
int main() {
Light l = Light::Red;
for (int i = 0; i < 4; i++) {
cout << name(l) << " -> ";
l = next(l);
}
cout << name(l) << "\n";
}red -> green -> yellow -> red -> green
Choosing the values
You can assign explicit values, and choose the underlying integer type after a colon. Unlisted values continue counting from the previous one:
#include <iostream>
#include <cstdint>
using namespace std;
enum class HttpStatus : int { Ok = 200, Created, NotFound = 404, ServerError = 500 };
enum class Level : std::uint8_t { Low = 1, Medium = 5, High = 10 };
int main() {
cout << static_cast<int>(HttpStatus::Created) << "\n";
cout << static_cast<int>(HttpStatus::NotFound) << "\n";
cout << static_cast<int>(Level::High) << "\n";
cout << sizeof(Level) << " byte\n";
}201
404
10
1 byte
Unscoped (classic) enums
The older enum without class puts its names straight into the surrounding scope and converts silently to int:
#include <iostream>
using namespace std;
enum Weekday { Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun };
int main() {
Weekday today = Fri;
int daysLeft = Sun - today; // implicit conversion to int
cout << "Day " << today << ", " << daysLeft << " days until Sunday\n";
}Day 5, 2 days until Sunday
That convenience comes at a price: two unscoped enums in the same scope cannot both have a value named Red, and nonsense like today * 3 compiles.
enum |
enum class |
|
|---|---|---|
| Names | leak into the surrounding scope | must be qualified: Light::Red |
Conversion to int |
implicit | requires static_cast |
| Recommended for new code | rarely | yes |
Tip: C++20 adds
using enum Light;inside a function orswitch, letting you writeRedinstead ofLight::Redlocally while keeping the type scoped everywhere else.
Test yourself
In enum class Size { S, M, L };, what is the underlying value of Size::L?
- 1
- 2
- 3
- 'L'
How do you print the number behind a scoped enum value c?
- cout << c;
- cout << int c;
- cout << static_cast<int>(c);
- cout << c.value;
Why is enum class preferred over plain enum?
- It is faster
- Its names do not leak and it does not convert implicitly to int
- It can store strings
- It allows floating-point values