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

How to use bitmask_operators.hpp with namespace and classes

I want to use C++11 enum class as bitfields and find a nice approach here. But I stuck, if my enum class declaration is not in global namespace but in custom namespace or inside of a class instead. E.g.: #define ENABLE_BIT_OPERATORS(E) template<>…
Joe
  • 3,090
  • 6
  • 37
  • 55
0
votes
0 answers

Is it possible to declare an enum which is part of a class in a separate place?

I have a class with an attribute type of a custom enum class Class::Type. Currently the declaration of Class looks like this: class Class { enum class Type { a, b, c, ... } type; }; But I want to put the…
user6245072
  • 2,051
  • 21
  • 34
0
votes
1 answer

I'm using :: operator but I still get error that enum class error is not a class, namespace, or scoped enum

I want to print my Car object's color and noise. I'm trying to use c++11's enum class within my Car class. When I compile I get error Car::Color and Car::Noise is not a class, namespace, or scoped enumeration. I am using the :: operator to access…
0
votes
2 answers

C++14 operator << for stream insertion and enum class

I am writing a game in which there are 2 players, "BLACK" and "WHITE". I have the following enum class: enum class PlayerType { BLACK, WHITE }; I would like to write an ostream operator<< for this class. Here is my attempt: std::ostream&…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
0
votes
2 answers

How to use SFINAE to make a polyfill for missing values in enum class?

I thought this should be easy, but I've been struggling with it for a while so I thought I should ask here. I want to make a template metafunction that takes a type corresponding to a C++11 enum class as its argument, and returns an int: If the…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
0
votes
2 answers

Inner scoped enumeration, hash function and unordered_set data member

I've the following problem of which I cannot find a solution. Of course, it could be that a solution does not exist at all, but I'd like to have a try on SO before to give up. First of all, a snippet that compiles with no errors: #include…
skypjack
  • 49,335
  • 19
  • 95
  • 187
0
votes
1 answer

define enum class with same name compared to enum

Why this compiles in c++11: struct foo { enum class Resolution { None=10, Nominal=20 }; enum class Scale { None, Nominal }; }; while this doesn't: struct foo { enum Resolution { None=10, Nominal=20 }; enum Scale { None, Nominal }; }; ?
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
0
votes
1 answer

GoogleTest 1.7.0 `enum class` compile error

When I try and reference an enum class from a test fixture, it fails to compile with error ./gtest_mcp23s17.cpp:25:52: error: no type named 'HW_ADDR_6' in 'mcp23s17::HardwareAddress' TC_mcp23s17 _gpio_x(mcp23s17::HardwareAddress::HW_ADDR_6); …
Zak
  • 12,213
  • 21
  • 59
  • 105
0
votes
2 answers

C++: How to get function to accept an object with same class name from any namespace?

MainClass.h: namespace Alpha{ enum class Parameters; } namespace Beta { enum class Parameters; } MainClass{ public: enum class Type{ Type_A, Type_B }; MainClass(const Type t); void DoStuff(const Parameters p); private: void doesStuff(const…
shavera
  • 803
  • 1
  • 8
  • 18
0
votes
0 answers

Why var.constant is not allowed for an enum class?

Suppose we have an enum class: enum class E { constant }; To refer to the enumerator in E, we can write E::constant, while the following is illegal: E e; e.constant; But consider this: struct S { enum {constant}; }; Both S::constant and…
Jamboree
  • 5,139
  • 2
  • 16
  • 36
0
votes
2 answers

Sequence of enumerators at compile time

Given a C++11 enum class, is there some templating or other construct to iterate, at compile-time, over the set of all enumerators? Could one define a template to e.g. initialize an array with all possible values of that enum type?
MvG
  • 57,380
  • 22
  • 148
  • 276
-1
votes
1 answer

is it possible to convert std::string into enum class in std::cin?

I'am new in cpp. Let's say we've got a class as follows: class Animal { public: enum class Group {mammal, bird, reptile}; private: Group group; } Now I'd like to initialise it in this way: Animal::Group g; std::cin >>…
Caparso
  • 39
  • 5
-1
votes
1 answer

How to initialize `std::set` in constructor parameter?

I have a construction that takes a std::set as a parameter. How do I initialize the set in the constructor parameter? Here's an minimal conceptual example. Actual implementation is much larger. #include enum class Fruits { APPLE,…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
-1
votes
1 answer

'enum struct' won't compile unless is included

I have code that contains the line enum struct cols: int8_t {red, blue, green}; When i compile this, i get errors: test.cpp:4:1: warning: elaborated-type-specifier for a scoped enum must not use the 'struct' keyword enum struct cols: int8_t {red,…
user1479670
  • 1,145
  • 3
  • 10
  • 22
-1
votes
1 answer

Is GCC correct to warn of format string mismatch with a scoped enum?

When using a scoped enum in a varargs context, it is defined to be passed as its underlying type, as answered in "Can I use enum class values as arguments to varargs functions?" As I understand it, this is the only circumstance in which a scoped…
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
1 2 3
12
13