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
8
votes
4 answers

C++11 mixing enum class and unsigned int in switch case will not compile

Why doesn't this code compile, and what can I do to make it compile? #include using namespace std; enum class myEnum : unsigned int { bar = 3 }; int main() { // your code goes here unsigned int v = 2; switch(v) { …
paulm
  • 5,629
  • 7
  • 47
  • 70
8
votes
1 answer

Using enum class with std::bitset

First of all I want a normal enumeration instead of a bit-based enumeration, because the amount of different enums will be beyond any integral type. I also want to take advantage of the type safety of C++11 enum class. To do so, the natural choice…
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
7
votes
3 answers

Neat way how to cyclically iterate 4 enum class values in both directions in C++?

I have: enum class orientation { North, East, South, West }; I want to rotate its instance left (North => West) and right (West => North). But I don't want to convert them to numbers, because it harms readability and intention and also…
Lukas Salich
  • 959
  • 2
  • 12
  • 30
7
votes
2 answers

Initialize a two-dimensional std::array of type enum class (C++11)

I have the following class in C++11: class MyTable { public: enum class EntryType { USED, FREE }; MyTable(EntryType value) { for (uint32_t i = 0; i < 10; ++i) { memset(_table[i].data(),…
itzhaki
  • 317
  • 2
  • 10
7
votes
5 answers

Use enum classes with Boost Test

I have an enum class that I would like to use in my unit tests: enum class MyEnumClass { MyEntryA, MyEntryB }; I would like to use it as follows: MyEnumClass myEnumValue = MyEnumClass::MyEntryA; BOOST_CHECK_EQUAL(myEnumValue,…
ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
7
votes
3 answers

How to Enable C++11 Features in Codelite

The following code compiles and runs in Xcode 5 and in Visual Studio 2013. I am interested in trying out Codelite, but Codelite will not compile the following program (a problem since I am working with scoped enums in my project). As far as I…
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
7
votes
2 answers

Is it possible to make a scoped enumeration ("enum class") contextually convertible to bool?

Let's say I have enum class Flags : std::uint16_t { None = 0, A = 0x0001, B = 0x0002, C = 0x0004 } inline Flags operator|(Flags lhs, Flags rhs) { return static_cast(static_cast(lhs) |…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
7
votes
2 answers

enum class in QVariant in QSettings

I have a problem with enum classes, QVariants and the QSettings class. There are enum class values that I want to store within a QVariant which goes into a QSettings instance. So, my code actually looks something like this: enum class Foo { …
CppChris
  • 1,226
  • 9
  • 14
7
votes
4 answers

What's an enum class and why should I care?

For one who has never written a line of C++11, and who has, at the moment, no opportunity to program in C++11, can you, in one short paragraph., tell me: What is an "enum class" and why do we need it?
sbi
  • 219,715
  • 46
  • 258
  • 445
6
votes
1 answer

Invalid use of incomplete type struct std::hash with unordered_map with std::pair of enum class as key

I want to use a unordered_map,std::uint8_t> for managing some pixelmap formats. Here the minimal code : #include #include #include #include #include…
coincoin
  • 4,595
  • 3
  • 23
  • 47
6
votes
3 answers

c++: enum inside of a class using "enum class"

What would be the right way to write an enum inside a class? I am writing conway's game of life code in c++. So i have a patterns class which contains the info about different kind of patterns: class Patterns { public: Patterns(); …
divine-code
  • 107
  • 2
  • 6
6
votes
2 answers

Why enumeration cannot be a template?

enumeration cannot be a template is the error given when I try to compile with BCC64 (based on Clang) the following code: template enum class fooEnum : T { a,b,c,d,e }; At first, I was thinking that this explicit prohibition was…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
6
votes
1 answer

The difference between enum and enum class?

I've recently started working the C++/CLI managed code, but I've always defined enums like so: enum FV_MODE { IDLE,DRAG,ADD_HITBOX,ADD_HURTBOX }; Until today, when I was hit with the error message: cannot define an unmanaged enum…
Guy Joel McLean
  • 1,019
  • 4
  • 12
  • 34
6
votes
2 answers

Link compatibility of enums and enum classes

Suppose there is a C++11 API that uses enum classes: // api.hpp enum class E {A, B, C}; void f(E); ... // api.cpp void f(E e) { if (e == E::A) ... } Now suppose I would like to use this API, but I don't have a C++11 compiler. So…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
5
votes
1 answer

Check if enum class contains a specific identfier

I searched a bit here on SO and was surprise that I didn't find any similar question. Happy for any hints in case this has already been answered. I have a codebase with a lot of enum classes defined. Some of them specify a totalNum constant…
PluginPenguin
  • 1,576
  • 11
  • 25
1 2
3
12 13