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
1
vote
3 answers

enum class in c++ can not compile in Mac, while works in Windows

it seems there are some problems with my g++ 4.2 compiler in my Mac. I defined a enum class as follows. enum class Suit {CLUBS,SPADES,HEARTS,DIAMONDS}; It can compile and run in VS2013 but failed to compile in my Mac (expected identifier or '{').…
ivory
  • 243
  • 4
  • 16
1
vote
0 answers

c++11: Type transformation of an enum-class-type derivative via alias-template

Consider the following code, which compiles fine in clang but not in gcc (4.7.2): template using remove_ref_typed = typename std::remove_reference::type; // alias-template w/ 'typename' template using remove_ref = …
etherice
  • 1,761
  • 15
  • 25
1
vote
1 answer

How is enum class similar to enum, or a class?

Is the type enum class a completely separate from a traditional class, or is its implementation similar? How does enum class work? The reason I ask is because I don't understand how it can be similar to both a class and an enum at the same time. I…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
1
vote
2 answers

Assigning values after defining an enumeration

I'm a newbie to C++ and especially C++11, so since I've now got to use it, a few questions about 'enum' and 'enum class' came up: Can I assign values after the enumeration has been declared? enum MyEnum; MyEnum::HELLO = 0; MyEnum::WORLD = 1; Can I…
Xaymar
  • 37
  • 9
1
vote
0 answers

How can define a ponctuation in an enum?

I use Stanford POStagger and I want to define an enum as bellow : public enum POSType { CC("Conjunction"), CD("Number"), ! ("Punctuation") ...... } How can I define a punctuation as an enum variable?
Mona
  • 11
  • 1
0
votes
0 answers

Change value of parameter of an item in enum class via putExtra()

I have a problem with changing value of parameter in android studio, kotlin. I want to change boolean value of a particular item in enum class. Here's the MainActivity.kt: // Variables val intentMainActivityToShow =…
0
votes
2 answers

How can I implicitly convert an enum to its subset and vice versa in C++?

More precisely, the feature I want is like implicitly convert an enum to its subset enum and vice versa. The code I wish it working: enum class Human { A = 1, B = 2, }; enum class Male { // subset of Human A = Human::A, }; enum…
ke_bitter
  • 9
  • 1
0
votes
1 answer

Function to return the value of a Map by key in an enum

I have an enum class which maps language locale to a list. How can I fix the function getReservationFrequencies inside the companion object to return the value of the map based on the locale (key)? enum class ReservationFrequencies(val frequencies:…
0
votes
1 answer

Can't manage to call is_unsigned on the underlying_type of an enum class template parameter

I'm attempting to limit the use of this function to enum classes that have unsigned underlying types (and have AddSubtract as an enumeration) but I cannot for the life of me figure out the correct syntax. template concept EnumAddSubtract…
GLJeff
  • 139
  • 10
0
votes
1 answer

Trying to create a generic cast to str for several enum classes

I'm trying to create a generic function toStr() for several enum classes but I'm having some issues. I have these enum classes and maps to convert the value of the enums to string. ''' enum class InitFields : int32_t { userRightsPath = 1, …
Ojotuno
  • 21
  • 6
0
votes
0 answers

odd behavior with enum struct and compound assignment operator

I observed some behavior relating to enum structs and compound operators that I don't understand (yet). When using an enum struct as a flag, I'd like to write stuff like flag &= enum_name::What. However, depending on the compilation this either…
rochus
  • 1
0
votes
1 answer

enum class nested in a class compiles on Linux, but not MacOS?

I wrote a basic program to show the issue I'm dealing with. #include using namespace std; class SomeClass { private: public: enum class SomeEnum : int {a, b, c}; void print() { cout << "test"; } }; int…
0
votes
2 answers

How to iterate over enumerators of an enum class?

Is there a way to initialize a container (e.g. std::unordered_set) with the enumerators of an enum class? I have this class: #include #include class Foo { public: inline static const std::unordered_set
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
1 answer

Is there an accepted way to adjust this so that all of my `enum class` elements are grouped together in the ClassView?

Old code: typedef enum tagUndoAction { UNDO_CHANGE_CELL = 0, UNDO_CHANGE_SELECTION_START, UNDO_CHANGE_SELECTION_SUB } UNDO_ACTION_E; New code: enum class UndoAction { ChangeCell = 0, …
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
0
votes
1 answer

Enum class to string

I’m having a solution with switch cases but there are many cases so clang-tidy is giving warning for that function. My motive is to decrease the size of function. Is there any way that we can do to decrease size of function.