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

enums and enum classes in Qt Queued Slots

Following advice from Qt documentation and this question's answers, I have code structured like so: emulator.h: class Emulator : public QObject { Q_OBJECT public: enum HaltCause { Breakpoint, ReadWatch, …
Brian A. Henning
  • 1,374
  • 9
  • 24
0
votes
1 answer

Template optimization for enum base type

I have the two code examples below. Both of them interprete an enum or enum class as their underlying type. Which one would be smaller after compilation when using multiple different enums? Working on a project for data serialization I need to cast…
Bart
  • 1,405
  • 6
  • 32
0
votes
0 answers

Placing definition of enum class, used as template parameter for a template class, inside the template class

I like to define my enum's that belong to a certain class inside that class. That makes it clear they belong together and helps against namespace pollution: class Bar { public: enum class Foo { something, somethingElse …
Unimportant
  • 2,076
  • 14
  • 19
0
votes
0 answers

Given a "black box" enum, is it possible to check if a provided integral is a valid member of the enum in C++?

I'm writing an EnumSet class for a project. I would like to have an EnumSet::AllOf() method if possible, like in Java. The obvious way to go about this is to iterate over every member of the std::underlying_type and try casting it to the enum class,…
Q Science
  • 111
  • 7
0
votes
2 answers

proper placement of a hash table for enum class

I am working on a senior project and having a question about how to best implement a lookup table for my program. There are several enum class files that contain enum classes and an operator<< overload to output them to std::strings. We are using…
jleibman
  • 157
  • 1
  • 9
0
votes
2 answers

How to reuse operator overloads for different types?

I have several enums defined as follows: enum class Suit { spades = 1, hearts, diamonds, clubs, first = spades, last = clubs }; enum class Rank { six = 6, seven, eight, nine, ten, jack, queen, king, ace, first = six, last =…
sergej
  • 17,147
  • 6
  • 52
  • 89
0
votes
1 answer

C++11 enum class namespace block

I have an enum class in C++11: enum class eDays{ SUNDAY, MONDAY, /*...*/ }; The enum class sets the namespace for the values so it has to be used like: eDays::SUNDAY I want to set an namespace block so I won't need to specify the namespace each…
igor
  • 716
  • 1
  • 9
  • 27
0
votes
1 answer

c++ Using a a template function with enum class and overloaded conversion operators

I was reading the sample code on another post Specializations only for C++ template function with enum non-type template parameter and I'm trying to take it one step further, by using a overloaded conversion operator to use the object as if it was…
0
votes
1 answer

Using enum class type in switch causing errors

I was hoping someone could help me out with an error I'm having using an enum class in a switch case. trying to use the traverse type to choose the path in the switch. Here is my code: enum class TraverseType { PREORDER, INORDER, POSTORDER,…
D. Spani
  • 13
  • 2
0
votes
2 answers

How to access enum class through index?

enum class Fruit { apple, orange, pear }; enum class Color { red, green, orange }; template struct Traits; //I have to return the appropriate value(string) of color and fruit in their respective structs. //I could do this by switch…
0
votes
3 answers

How can I write an enum class to a file and access it in C++?

I am trying to write the contents of a class object into a file. The object has an enum class member and I am unable to write it into a file using ofstream. I get the following error. error: no match for ‘operator<<’ (operand types are…
Abhishek V
  • 21
  • 1
0
votes
1 answer

How to check values inside enum class?

I wanted to check if the tags returned from a json object has one of the values inside an enum class. Gson gson = new Gson(); AnalysisResult result = gson.fromJson(data, AnalysisResult.class); for(Enum p : Enum.values()){ …
joycecruz
  • 23
  • 5
0
votes
1 answer

Why is the constructor undefined?

Even though I'm working with Bukkit, this is a Java problem; I don't know, why Java says the constructor is undefined, since it is defined import org.bukkit.entity.EntityType; import net.minecraft.server.v1_10_R1.EntityCow; import…
0
votes
0 answers

C++ Hide enum class from rest of code

I have some code with an enum class in it. I only want there to exist a single instance of this enum class. I am aware of singleton classes, but is there a way to make an enum class only visible from within the current file? Trivial example: enum…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
0
votes
2 answers

Singleton (Traditional vs Enum)

Disclaimer: Apologies if this question is too basic. I'm learning about Singleton and have a quick question its implementation, are these differences purely coding preferences or am I missing something? Singleton Class public enum SerialNumberGen…
Glaube
  • 19
  • 7