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
4
votes
1 answer

Modifying scoped enum by reference

I am increasingly finding scoped enums unwieldy to use. I am trying to write a set of function overloads including a template for scoped enums that sets/initializes a value by reference--something like this: void set_value(int& val); void…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
4
votes
2 answers

C++11, enum class, undefined reference with g++, works with clang++

I used the new C++11 "enum class" type and observed a "undefined reference" problem when using g++. This probleme does not happen with clang++. I do not know if I am doing something wrong or if it is a g++ bug. To reproduce the problem here is the…
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
4
votes
1 answer

C++11 enum class instantiation

I've encountered the following form of enum class variable instantiation and it is compiling without any warning or error under VS2012: UINT32 id; enum class X {apple, pear, orange}; X myX = X(id); Moreover, sending X(id) as an argument to a…
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
4
votes
1 answer

Is self-documenting code worth potential performance issues?

I created small class that allows me to use enumerators of strongly-typed enums as flags (in combination). I'm using type_traits for underlying type detection so it should be also slightly type safe and mostly processed at compile time. However, i…
3
votes
2 answers

Is using enum class for flags undefined behavior?

I've been using overloaded operators as demonstrated in the second answer from here: How to use C++11 enum class for flags ... example: #define ENUMFLAGOPS(EnumName)\ [[nodiscard]] __forceinline EnumName operator|(EnumName lhs, EnumName rhs)\ {\ …
GLJeff
  • 139
  • 10
3
votes
1 answer

why does define conflict with enum class?

Here is a small code in which the conflict occurs. is there any way to get around this correctly? #define DEBUG 0 enum class TypeEnum : int { DEBUG = 0, INFO = 1 };
3
votes
2 answers

Convert Enum value to integer in "Enum" and "Enum Class"

What is the difference between the Enum and Enum Class and how to converting Enum value to the integer in "Enum" and "Enum Class"?
Mohammad reza Kashi
  • 321
  • 1
  • 5
  • 17
3
votes
1 answer

What's the difference between typedef enum and enum class?

I'm probably missing something but I'm working with a code base that uses a lot of typedef enum foo { .... } foo; Is this just the same as an enum class but not strongly typed?
Joshua Williams
  • 155
  • 1
  • 7
3
votes
2 answers

"Other"/Default-name behavior on a python Enum

I'm trying to achieve the following behavior in a python "enum" (so far to no success): Given the enum class class MyEnum(enum.Enum): A=1 B=2 C=3 I want to have an "Other" member such that MyEnum(5) will be interpreted as "Other", while…
EZLearner
  • 1,614
  • 16
  • 25
3
votes
1 answer

What is the C++ equivalent of Java's EnumSet.allOf()?

I'm writing a simple Naive Bayes image classifier in C++. I'd like to parametrize it by two enum class types (one for the type of input pixels, one for the class of the image). The problem is that part of the spec requires me to be able to write the…
Q Science
  • 111
  • 7
3
votes
2 answers

What is happening when calling an enum/enum class with parentheses in C++?

This is maybe a bit weird question, but I really don't know how to phrase it better than this. I just discovered that I can do the following: #include enum class Colour // also works with plain old enum { Red = 1, Green, …
dosvarog
  • 694
  • 1
  • 6
  • 20
3
votes
3 answers

Define enum element value using an operation on another enum values

Here is what I need to do: define, inside a class, two enumerations, the second having elements defined using elements values from the first. So something like this: class MyClass { public: enum class Elem { A=1, B=2, C=4,…
Silverspur
  • 891
  • 1
  • 12
  • 33
3
votes
1 answer

c++11: enum member initialization

I have a class defined as follows struct X { X() : data() {} int data; enum class Zzz : int { zero, one, two }; Zzz zzz; }; ... X xval; What is the value of xval.zzz - is undefined or X::Zzz.zero ? I know it will be undefined for…
uuu777
  • 765
  • 4
  • 21
3
votes
0 answers

"using" directive for declarator of scoped enum?

Is there a way to use some kind of using directive to directly access members of an enum class type? enum class Foo { Foo1, Foo2, ... }; int main() { auto foo = Foo::Foo1; ??? // magic command to make the lines below work …
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
3
votes
1 answer

Using enum class for defining flags

What is the appropriate pattern for using enumerations as flags in modern C++? The question stems from my reading of the technical specification A Proposal to Add 2D Graphics Rendering and Display to C++, where McLaughlin, Sutter, and Zink propose a…
Escualo
  • 40,844
  • 23
  • 87
  • 135