Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

enum — enumeration type

The enum keyword defines a distinct type with named constants. Learn the differences between enum and enum class, value numbering, and underlying-type selection.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

enum Name { First, Second = 4, Third };
enum class Name : unsigned int { First, Second = 4, Third };
enum struct Name : unsigned int { First, Second = 4, Third };

enum defines a distinct type with named constants. Without an initializer, the first constant is 0; subsequent constants increment the previous value by 1.

Plain enum also exposes enumerator names in the enclosing scope and permits implicit integer conversion. Equivalent enum class and enum struct keep names in enumeration scope, such as State::done, without that conversion.

: unsigned int fixes the underlying type. Without it, scoped enumerations use int; unscoped ones use an implementation-selected type. Enumeration values are not necessarily limited to named constants; casting does not replace validation.

Entry scope: C++23. The linked source is an evolving draft, not a frozen C++23 edition.

Examples

#include <iostream>

enum Legacy { off, on };
enum class State : unsigned int { idle, running = 4, done };

int main() {
    int enabled = on;
    State state = State::done;
    static_assert(static_cast<unsigned int>(State::idle) == 0);
    static_assert(static_cast<unsigned int>(State::done) == 5);
    std::cout << enabled << ' '
              << static_cast<unsigned int>(state) << '\n';
}

Result

1 5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources