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
140
votes
9 answers

How can I output the value of an enum class in C++11

How can I output the value of an enum class in C++11? In C++03 it's like this: #include using namespace std; enum A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } in c++0x this code doesn't…
Adi
  • 2,011
  • 2
  • 15
  • 14
120
votes
18 answers

Is it possible to determine the number of elements of a c++ enum class?

Is it possible to determine the cardinality of a c++ enum class: enum class Example { A, B, C, D, E }; I tried to use sizeof, however, it returns the size of an enum element. sizeof(Example); // Returns 4 (on my architecture) Is there a standard…
bquenin
  • 1,470
  • 2
  • 12
  • 12
93
votes
8 answers

Can't use enum class as unordered_map key

I have a class containing an enum class. class Shader { public: enum class Type { Vertex = GL_VERTEX_SHADER, Geometry = GL_GEOMETRY_SHADER, Fragment = GL_FRAGMENT_SHADER }; //... Then, when I implement the…
Appleshell
  • 7,088
  • 6
  • 47
  • 96
54
votes
3 answers

Is it possible to manually define a conversion for an enum class?

Normally you can define a cast for a class by using the following syntax: class Test { public: explicit operator bool() { return false; } }; Is there a way to do this or something similar for an enum class?
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
47
votes
1 answer

Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword

I have the following enum specification: enum class FaceDirection : int8 { Down, Up }; g++ 4.8.1 gives the following error: warning: elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword What causes this?
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
44
votes
3 answers

User Defined C++11 enum class Default Constructor

Is there a way to specify the default constructor of an enum class? I am using an enum class to specify a set of values which are allowable for a particular datatype in a library: in this case, it's the GPIO pin id numbers of a Raspberry Pi. It…
FizzixNerd
  • 609
  • 1
  • 5
  • 9
42
votes
3 answers

Implementation of operators for enum class

Following the discussion in question Incrementation and decrementation of “enum class”, I'd like to ask about the possible implementation of arithmetic operators for enum class types. Example from the original question: enum class Colors { Black,…
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
39
votes
5 answers

C++11 standard conformant bitmasks using enum class

Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r)…
B.S.
  • 1,435
  • 2
  • 12
  • 18
30
votes
6 answers

Why can't C++11 strongly-typed enum be cast to underlying type via pointer?

In C++11 we can cast a strongly-typed enum (enum class) to its underlying type. But it seems we cannot cast a pointer to the same: enum class MyEnum : int {}; int main() { MyEnum me; int iv = static_cast(me); // works int* ip =…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
20
votes
1 answer

Forward Declaring enum class not working

In State.h I have enum class StateID : unsigned int; In State.cpp I have enum class StateID : unsigned int { NullID = 0, MainMenuID, GamePlayID, }; The problem is that any class that includes State.h has the forward declaration, but I…
EddieV223
  • 5,085
  • 11
  • 36
  • 38
18
votes
3 answers

enum class of type string in C++

- Background Information: There is a class in C++11 known as enum class which you can store variables inside. However, I have only seen the type of the class to be char: enum class : char { v1 = 'x', v2 = 'y' }; - Question: Is there any way I…
Agent 0
  • 361
  • 1
  • 5
  • 17
18
votes
3 answers

Qt - Q_DECLARE_METATYPE() with an enum class type

Is there a way to use Q_DECLARE_METATYPE() with enum class types? I know old enums work but what about these new, strongly typed, ones? Can't find anything regarding this problem elsewhere. I'm using the latest Qt version available. Example: enum…
Venom
  • 1,107
  • 4
  • 21
  • 46
18
votes
3 answers

Is it safe to reinterpret_cast an enum class variable to a reference of the underlying type?

I've seen reinterpret_cast used to apply incrementation to enum classes, and I'd like to know if this usage is acceptable in standard C++. enum class Foo : int8_t { Bar1, Bar2, Bar3, Bar4, First = Bar1, Last = Bar4 }; for…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
15
votes
4 answers

Wrap enum class with Cython

I am trying to wrap an enum class in a c++ header file for use in a cython project. For example, how can this enum class Color {red, green = 20, blue}; be wrapped with Cython.
user3684792
  • 2,542
  • 2
  • 18
  • 23
15
votes
2 answers

Enum Class "could not convert to unsigned int"

I have an enum class like this: typedef unsigned int binary_instructions_t; enum class BinaryInstructions : binary_instructions_t { END_INSTRUCTION = 0x0, RESET, SET_STEP_TIME, SET_STOP_TIME, …
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
1
2 3
12 13