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
5
votes
2 answers

Can you use bitwise operators with enum classes without casting?

I like to use enum classes, but I use them as flags sometimes and I have to constantly cast to int if I want to use a bitwise operator. Is there a way to do this without casting? I don't think you can define the operators for them? If I have…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
5
votes
3 answers

Handling of switch enum class returns in clang, gcc and icc consistently

I am generally using clang to develop code, using all reasonable warnings I can (-Wall -Wextra [-Wpedantic]). One of the nice things about this setup is that the compiler checks for the consistency of the switch stataments in relation to the…
alfC
  • 14,261
  • 4
  • 67
  • 118
5
votes
1 answer

Use 'using enum' in C++20 in classes possible?

In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace. I was wondering if that also means that I can also use it within class…
the_summer
  • 439
  • 3
  • 10
5
votes
0 answers

How to check if a type is an enum class?

With the following code I can check at compile time if type E is an enum: static_assert(true == std::is_enum::value, "Must be an enum"); How can I check if it is an enum class? Here they suggest to add the check !std::is_convertible
Pietro
  • 12,086
  • 26
  • 100
  • 193
5
votes
1 answer

Type cast failed in switch for enum with restricted storage

SSCCE: enum class confirm {yes}; struct item { confirm s:4; // (1) limiting storage size required }; int main() { item itm; itm.s = confirm::yes; // (2) OK switch (itm.s) { case confirm::yes: // (3) Failure, need static data cast…
dyomas
  • 700
  • 5
  • 13
5
votes
1 answer

User-defined implicit conversion of an enum class when calling an overloaded operator fails

Consider the following example: struct ConvertibleStruct {}; enum class ConvertibleEC {}; struct Target { // Implicit conversion constructors Target(ConvertibleStruct) {} Target(ConvertibleEC) {} }; Target operator~(const Target& t)…
5
votes
1 answer

Implicit conversion from int to enum class in switch statement

enum class pid { Alpha, Beta, Gamma }; int main() { int propId = 2; switch(propId) { case pid::Alpha: case pid::Beta: case pid::Gamma: break; } } Above snippet compiles fine in msvc2012 (and works) but fails…
kozmo
  • 51
  • 1
  • 4
5
votes
2 answers

How to get enum from boost::property_tree?

How do I get an enum from a boost::property_tree? This is my "non-working" example. config.xml EMISSION::EMIT1 42 main.cpp #include #include…
Raymond Valdes
  • 1,161
  • 1
  • 9
  • 15
5
votes
2 answers

typedef and enum or enum class

I have an enum like this: (Actually, it's an enum class) enum class truth_enum { my_true = 1, my_false = 0 }; I would like to be able to expose my_true to the global namespace, so that I can do this: char a_flag = my_true; Or at…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
5
votes
1 answer

Static import in C++11 (e.g. an enum class)

My usage of enum class (VS2012): class matrix { public: enum class operation_type {ADD, MULT}; matrix(operation_type op); ... } and in another fragment I use matrix* m = new matrix(matrix::operation_type::ADD); If the names are long, this…
Joshua MN
  • 1,486
  • 2
  • 13
  • 26
5
votes
2 answers

Misunderstanding of range-based for loop?

A compiler error occurs when I try to compile the following code: for(binary_instructions_t &inst: BinaryInstructions){ } BinaryInstructions is this enum class: typedef unsigned int binary_instructions_t; enum class BinaryInstructions :…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
5
votes
2 answers

C++11 enum with class members and constexpr link-time optimization

In my project I have a lot of enumerations that need to have additional attributes associated with the enumeration members and auxiliary static methods associated with the enumeration type. As much as I know, this is not possible to have with the…
David L.
  • 3,149
  • 2
  • 26
  • 28
4
votes
2 answers

Dispatch on execution policy by type or enum?

In C++ I basically have two choices in policy based design patterns: I can use individual types (based on which an overload is selected) or specify an enum that contains all policies and I would dispatch over them at runtime. What is the preferred…
glades
  • 3,778
  • 1
  • 12
  • 34
4
votes
2 answers

Scoped Enums (enum class) relational operators

I've looked all over the place and I can't believe this question has not been asked before. Is the ordering of scoped enumerators defined by the standard? Say if I have the following #include enum class Fruits {Apple, Orange,…
GamefanA
  • 1,555
  • 2
  • 16
  • 23
4
votes
1 answer

QML component enum class property

I have delegate component in a separate qml file, in which I'd like to have a property which is an enum class type coming from a c++ QObject. Is this possible? Here is a Minimum (non)Working Example: card.h #include class Card : public…
speter
  • 49
  • 2
  • 7