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

How to forward declare enum class as inner class of a templated class?

I'm abstracting the interrupt vector table on multiple microcontrollers. I am using template classes in the form of (InterruptVectorTable.hpp(definition, included in implementation)) template InterruptVectorTable { …
1
vote
0 answers

Testing against values of an enum class in google test

I have a constructor of an object that can throw an exception if a parameter of an enum class type is wrong. This permits me to ensure that such an object, that will use that parameter for some decision, is never constructed if the value of this…
Zack
  • 117
  • 1
  • 11
1
vote
1 answer

Operator overloading of enum class in class

I'm using private enum class in nested class and want to implement operator! for my enum class. I know how to do this. But when I tried to overlord operator of enum class in nested class, compiler treats my operator as an operator of class, not for…
1
vote
2 answers

Keys to access vector fields - enum class or enum in namespace?

Let's say I have an alias for vector: typedef std::vector PlanetData; And I want it's fields to be accessible via some keys: double x = planet_data[PlanetDataKeys::PosX]; //planet_data is of type PlanetData How can I achieve it? I can…
1
vote
2 answers

The out of range value of enum class

When I define an enum class inside a function, it has a value from the available options. However, when I define it within a class, it has value of none of the options. So what is the initial value of g.f? what will return true when compared?…
ar2015
  • 5,558
  • 8
  • 53
  • 110
1
vote
1 answer

Implicit convertion of custom class to enum classes

I want to create a class, that will be implicitly convertible to selected enum classes, in order to switch over it. But following code does not compile (gcc). Compiler complains that conversion is ambiguous, but it does not compile with single…
omicronns
  • 357
  • 1
  • 10
1
vote
1 answer

Boost test fails with enum classes inside namespaces

If you define an operator << for a C++11 enum class, then you can use it successfully with Boost's unit test library. However if you put the enum class inside a namespace, the Boost code no longer compiles. Why does putting the enum class inside a…
Malvineous
  • 25,144
  • 16
  • 116
  • 151
1
vote
0 answers

Getting errors because compiler confuses static enum class between C++11 and Managed C++

I have recently migrated a Managed C++ project from Visual Studio 2010 to 2013. Inside the definition of a class, I have: static enum class ItemType { SHOP=IDS_SHOP, PAGE=IDS_PAGE, PHOTO=IDS_PHOTO }; static bool EnumTryParse(CString…
sergiol
  • 4,122
  • 4
  • 47
  • 81
1
vote
1 answer

enum class ambiguous inheritance

I am really strugling with an inheritance issue when using what should be strongly typed enums - however the enums seems to be ambigious when used through inheritance. Im using g++ (GCC) 4.7.2 enum class AE { V1 }; enum class BE { V2 }; enum class…
Tobibobi
  • 53
  • 6
1
vote
0 answers

Conditional enum as part of templated class

I am working on my c++ library for microcontrollers and I need a way to specify different enum class content based on the template type. I have this code so far: #include #include enum class timer_e { timer1, …
1
vote
1 answer

Enum class bitmasks used in template constexpr method

I have the following C++11 code in my microcontroller project: template struct mask_or; template<> struct mask_or<> { static constexpr std::uint32_t value = 0; }; template struct…
1
vote
1 answer

c++ typedef/type substitution for enumeration class

As far as I am aware at the moment it is not possible to do a typedef of the C++11 enum class. I would like to know if there is any other way I can reduce the length of the name of a variable enum when referring to it outside of the encapsulating…
user1391279
1
vote
2 answers

Is there a simple way to convert an enum class to a string (c++)?

while there are solutions to easily convert an enum to a string, I would like the extra safety benefits of using enum class. Is there a simple way to convert an enum class to a string? (The solution given doesn't work, as enum class can't index an…
Mike H-R
  • 7,726
  • 5
  • 43
  • 65
1
vote
0 answers

Astyle C++ enum class indentation

Is there a way of getting Astyle to indent enum class elements to the same depth? I am getting the following: enum class test { test1 = 1, test2 = 2, test3 = 3, test4 = 4 …
John
  • 10,837
  • 17
  • 78
  • 141
1
vote
1 answer

enum input from xml to c++ program using boost::property_tree

How do you read a enum-class from an XML file using the boost::property_tree library? I would like to avoid reading it as a string and mapping the string to the enum-class in my program.
Raymond Valdes
  • 1,161
  • 1
  • 9
  • 15