`enum` — C++ Keyword
`enum` — C++ Keyword
The enum keyword in C++: defines an enumeration type for named integer constants.
`enum` — C++ Keyword
The enum keyword in C++: defines an enumeration type for named integer constants.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
enumDefines an enumeration type. C++ provides unscoped enumerations (enum) and scoped enumerations (enum class / enum struct, C++11).
enum Name { enumerator1, enumerator2 }; // unscoped
enum Name : underlying-type { ... }; // explicit underlying type
enum class Name { enumerator1, enumerator2 }; // scoped (C++11)
enum struct Name : underlying-type { ... }; // scoped with underlying type
#include <print>
// Unscoped: enumerators leak into enclosing scope
enum Color { Red, Green, Blue };
// Scoped: access requires Direction::North
enum class Direction : int { North = 0, South, East, West };
int main() {
Color c = Green; // unscoped – name is plain Green
std::println("{}", static_cast<int>(c)); // 1
Direction d = Direction::North;
std::println("{}", static_cast<int>(d)); // 0
}
enum class in new code: scoped enumerations prevent name collisions and implicit conversions.int; specify it explicitly for ABI stability.enumint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}