`enum` — C++ Keyword

`enum` — C++ Keyword

The enum keyword in C++: defines an enumeration type for named integer constants.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

enum

Defines an enumeration type. C++ provides unscoped enumerations (enum) and scoped enumerations (enum class / enum struct, C++11).

Syntax

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

Example

#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
}

Notes

Example in practice

int 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;
}