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
0 answers

Unable to Intialize Enum Class Types | Visual C++

Consider an enum class enum class FOO { A,B,C }; struct something { FOO abc = FOO::A; //Compiler Doesnt like this } int main(){ something _something; return 0; } So the compiler doesnt like the initialisation and gives me 3 different…
0
votes
1 answer

initializing enum with a constructor

I already found this really good explanation Initialising enum via constructors but it didn't fit my needs. So I declare an enum inside a class and want to initialize it inside the class constructor and then call this enum via a switch statement…
sisso
  • 3
  • 2
0
votes
0 answers

Alternative for casting strongly-typed enum to int in C++

To convert a strongly-typed enum to int we can use: enum class MyEnum { a, b }; int x = static_cast(MyEnum::a); what if i use the following line, which is shorter: int x = int(MyEnum::a); My motivation: I need to cast my scoped enums and my…
qwa
  • 123
  • 10
0
votes
2 answers

How to return an enum by item index as apposed to value index?

How do I index into an enum via "item index" instead of the "value indexing": "Value indexing" (not useful to me with what I am doing): eSerialBauds eBaud = static_cast(1200); // yields eBaud1200 I want "item indexing": ( How to get…
jdl
  • 6,151
  • 19
  • 83
  • 132
0
votes
2 answers

Implicit conversion operator function from bool to enum class

I have a function to change the state of an LED that takes in an enum argument with three possible values: enum class Command { Off, On, Toggle }; void led(Command); I'd like to be able to use the led() function with bool arguments: led(true) or…
Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45
0
votes
2 answers

why we cannot print the value of enum class as enum in c++

Getting error while trying to print the enum class object. I am getting the error while trying to print this. where am I doing mistake? #include using namespace std; int main() { enum…
0
votes
0 answers

Access an array/map with index of enum class type (something like a constexpr map)

Say I have a enum class foo : std::uint32_t { a = 4711, b = 815, ... }; with n enumeration constants. Assume that there actual numerical value is important. I need to pass precisely one value of type T to a function for every…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
0
votes
1 answer

Expose a private std::bitset field that is inside a class for modification

I'm coding in a C++ project that hasn't advanced beyond C++11 yet. Let's say I have an enum class as follows: enum class Weekdays { kSunday = 0, kMonday, ... kSaturday, }; I want to create a class that has a private std::bitset…
0
votes
0 answers

Operator[] for map failing to compile with use of scoped enum as key, but it's at() function works. Why the ambiguity?

I have code that looks like this: enum class MyTypes { TYPE_1 = 1, TYPE_2 = 2, TYPE_3 = 3 }; static const std::regex reg1("some unique expression"); static const std::regex reg2("some unique expression"); static const std::regex reg3("some…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
0
votes
4 answers

why can enum class values of type int not be used as int

I wanted to change an old-style enum to enum class : int because of its own scope. But the compiler complains about using the values in integer arithmetics. But why - since the enum is explicitly typed as int? example: enum class MsgType : int { …
vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0
votes
5 answers

How do I switch over an enum class?

Enum classes are supposed to be strong enums in the sense that they don't implicitly convert to and from int. For instance: enum class EC { a, b }; However, when switching over such a "strong enum": int sw(EC ec) { switch (ec) { case EC::a:…
bitmask
  • 32,434
  • 14
  • 99
  • 159
0
votes
1 answer

Forward declared enum as class member variable

As a rule of thumb on forward declaration (from "API Design for C++", p. 214), I only include the header of a class if I: use an object of that class as a data member in my own class, or inherit from that class. In all rest cases I just forward…
gmargari
  • 171
  • 1
  • 9
0
votes
2 answers

How to include enum class as class template arguments?

I'm trying to write a generic container class that I can use as a library. I want to use it to instantiate each object in a collection exactly once. So I thought to pass in both the pointer to the new instance and an accomodating enum class to be…
Paauwer
  • 3
  • 2
0
votes
1 answer

Enum Class Operator Overload no match found for operator

I'm attempting to overload some operators for an Enum class. I get a compiler error saying it's unable to find the operator In Enum.h enum class SomeEnum : unsigned { Test0 = 0, Test1 = (1 << 0), Test2 = (1 << 1), }; In…
zsnafu
  • 332
  • 3
  • 13
0
votes
1 answer

Brace initialised static const unordered_map using enum class

I'm having issues using an enum class as a type specifier within an unordered_map. I've trawled the internet but had no luck with any of the solutions. Closest example I found was at the link below, but seemingly doesn't work. Can't use enum class…