Skip to content
This repository was archived by the owner on May 28, 2026. It is now read-only.

Latest commit

 

History

History
40 lines (35 loc) · 1.43 KB

File metadata and controls

40 lines (35 loc) · 1.43 KB

Scoped and Unscoped Enumerations

In C++, enum introduces an unscoped enumeration, and enum class or enum struct introduce a scoped enumeration.

enum class Fruit : char {
    NONE, // enumerator-list, NONE = 0
    APPLE = 'a',
    BANANA = 'b'
};
Fruit apple = Fruit::APPLE;

Scoped Enumeration (C++-only)

?inline

  • underlying type is int
  • requires enum-name:: to access
  • only =, == and != defined (operator overloading possible)

Unscoped Enumeration

?inline

  • underlying type is impl.-defined
  • access without enum-name
  • behaves like list of constants in surrounding scope
  • inherits ops. from underlying type

See Also

<:cppreference:875716540929015908> Enumeration Declaration
<:cppreference:875716540929015908> Using-enum-declaration (since C++20)
<:cppreference:875716540929015908> std::underlying_type (since C++11)
<:cppreference:875716540929015908> std::to_underlying (since C++23)
learncpp.com: Enumerated Types

?creditFooter 164892896514801664