Questions tagged [enum-class]

Enum classes combine aspects of traditional enumerations (names values) with aspects of classes (scoped members and absence of conversions). Being able to specify the underlying type allow simpler interoperability and guaranteed sizes of enumerations and also enables forward declaration.

Enum classes address three problems with traditional C++ enumerations:

  • Implicit int conversion.
  • Enumerators exported to the surrounding scope.
  • Their underlying type cannot be specified.

enum class are strongly typed and scoped:

// traditional enum
enum Alert { green, yellow, orange, red };

// scoped and strongly typed enum
// no export of enumerator names into enclosing scope
enum class Color { red, blue };   

// no implicit conversion to int
enum class TrafficLight { red, yellow, green };

Alert a = 7;              // error (as ever in C++)
Color c = 7;              // error: no int->Color conversion

int a2 = red;             // ok: Alert->int conversion
int a3 = Alert::red;      // error in C++98; ok in C++11
int a4 = blue;            // error: blue not in scope
int a5 = Color::blue;     // error: not Color->int conversion

Color a6 = Color::blue;   // ok

underlying type can be specified

Being able to specify the underlying type allow simpler interoperability and guaranteed sizes of enumerations:

enum class Color : char { red, blue };

// by default, the underlying type is int
enum class TrafficLight { red, yellow, green };

// how big is an E? (whatever the rules say; i.e. "implementation defined")
enum E { E1 = 1, E2 = 2, Ebig = 0xFFFFFFF0U };   

// now we can be specific
enum EE : unsigned long { EE1 = 1, EE2 = 2, EEbig = 0xFFFFFFF0U };

forward declaration enabled

enum class Color_code : char;     // (forward) declaration
void foobar(Color_code* p);       // use of forward declaration

enum class Color_code : char { red, yellow, green, blue }; // definition
182 questions
13
votes
2 answers

Enum bitfield and aggregate initialization

The following code is accepted by clang 6.0.0 but rejected by gcc 8.2 enum class E { Good, Bad, }; struct S { E e : 2; int dummy; }; S f() { return {E::Good, 100}; } Live godbolt example The GCC complains error: could not convert '{Good,…
Kan Li
  • 8,557
  • 8
  • 53
  • 93
13
votes
6 answers

std::get using enum class as template argument

I'm using a std::tuple and defined a class enum to somehow "naming" each of the tuple's fields, forgetting about their actual indexes. So instead of doing this: std::tuple tup; /* ... */ std::get<0>(tup) = bleh; // was it 0, or 1? I did…
mfontanini
  • 21,410
  • 4
  • 65
  • 73
11
votes
2 answers

Adding bitwise operations and conversion-to-bool to scoped enums - a Christmastide exploration

Let's say that I'm crazy and decided to create the following monstrosity: #include #include // Utility proxy type - convertible back to E but also permits bool conversion // for use in conditions. // // e.g. // Foo f =…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
10
votes
1 answer

How to make enum class to work with the 'bit-or' feature?

I usually use enum with the 'bit-or' or | together to allow an object has some options. How to make enum class to work with the 'bit-or' feature?
user1899020
  • 13,167
  • 21
  • 79
  • 154
10
votes
2 answers

Enum class C++11 by reference or value

I have basically two questions may be they are related so I'll put them into one. Should we pass enum class in C++11 by reference or value when passing to function. It is sort of inheriting primitive type but is it the whole object that is passed?…
abumusamq
  • 780
  • 1
  • 10
  • 29
9
votes
3 answers

validate integer is some enum class item (C++11)

i have some enum class enum class Foo { A=1, B=18 , Z=42 }; i want to check if some integer can be converted into a Foo. What would be the ideal way to do this? this is for runtime check (the integer is not known yet at compile-time) Obviously i…
lurscher
  • 25,930
  • 29
  • 122
  • 185
9
votes
3 answers

Can I use enum class values as arguments to varargs functions?

C++11 adds enum classes, which are stronger-typed enums - values of enum classes will not be implicitly converted to values of other enum classes or integers, and forward-declarations are permitted by virtue of an explicit size specifier. Is it…
user79758
9
votes
2 answers

C++ enum class: Cast to non existing entry

I have this situation on one Project where we have some socket-communication that mainly exchanges characters for flow-control. We cast those characters to an enum class : char in a switch. I was wondering, what might happen, if the other end sends…
bam
  • 954
  • 8
  • 26
9
votes
1 answer

Initialization of a static constexpr class member of enum-class type by explicit conversion function

I have a discrepancy between the behaviour of g++ 4.8.1 and clang++ 3.4. I've got a class A, of literal type, that has an explicit constexpr conversion function to type enum class E. Gcc allows me to initialize constexpr variables of type E from a…
je4d
  • 7,628
  • 32
  • 46
9
votes
5 answers

Can enum class be nested?

Can this be done? enum A { enum B { SOMETHING1, SOMETHING2 }; enum C { SOMETHING3, SOMETHING4 }; }; If not is there an alternative solution? The purpose of this question: Want/need to be able…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
8
votes
1 answer

Statically distinguish between an enum and an enum class in C++?

I have an event handler class that uses a template argument to set the event type. I want to enforce these event types to be enum classes of one byte size. Static assertion against the size is not an issue, but I cannot find information online on…
Smartskaft2
  • 468
  • 3
  • 16
8
votes
1 answer

Class vs enum class as an index type

P0138R2 proposal begins with1 There is an incredibly useful technique for introducing a new integer type that is almost an exact copy, yet distinct type in modern C++11 programs: an enum class with an explicitly specified underlying type.…
Evg
  • 25,259
  • 5
  • 41
  • 83
8
votes
3 answers

C++ enum class std::size_t implicit conversion

I have defined a tuple and its indices by creating an enum class: /** parameter { key ; value1 ; value1 ; } */ using Parameter = std::tuple; enum class ParameterKey : std::size_t { KEY = 0, VALUE1 = 1, …
sukovanej
  • 658
  • 2
  • 8
  • 18
8
votes
2 answers

Why can a lambda expression return a local enum class type?

Why and how does this work? What type is 'auto' here? auto lambda = [](){ enum class Local { X=0 }; return Local::X; }; auto x = lambda(); // No error! Why and what type is auto in this case? auto y = Local::X; // Error! Of course! The…
cwschmidt
  • 1,194
  • 11
  • 17
8
votes
1 answer

strongly typed C++0x enumeration comparison

why aren't instances of strongly typed C++0x enumerations comparable to each other? Update: They are comparable in gcc 4.6; I'm not sure if it worked in gcc 4.4.
Neil G
  • 32,138
  • 39
  • 156
  • 257
1
2
3
12 13