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
2
votes
2 answers

How to std::map?

I am trying to std::map, with an enum class and a std::string but I am getting some error. I am using gcc 4.4.7 with -std=c++0x (this is fixed) At .h file: enum class state_t{ unknown, off, on, fault }; typedef…
user2357667
  • 79
  • 1
  • 1
  • 9
2
votes
1 answer

Unhandled enum class value in switch() - Exception or Assert?

lvl is an enum class. switch(lvl) { case LogLevel::Trace: return "Trace"; case LogLevel::Debug: return "Debug"; case LogLevel::Info: return "Info"; case LogLevel::Warning: return "Warning"; case LogLevel::Error: return…
SF.
  • 13,549
  • 14
  • 71
  • 107
2
votes
1 answer

enum class and boost serialization

How can I serialize/deserialize a class that has a member of enum class using boost serialization. Example: enum class enum_class{ item1=0,item2=1 } class foo{ private: friend class boost::serialization::access; public: foo() =…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
2
votes
1 answer

Using `reinterpret_cast` on an enum class - valid or undefined behavior?

#include #include #include template using Underlying = std::underlying_type_t; enum class ETest : int { Zero = 0, One = 1, Two = 2 }; template auto& castEnum(T& mX)…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
2
votes
1 answer

Wrap legacy c enum to c++11 enum class

I have an old C library and I have to wrap it inside a C++ environment. I use C++11 enum class because they're so useful and I'd like to do transform the original enum into a new enum class without simply create the new enum class and rewriting it.…
user2714602
  • 231
  • 4
  • 16
2
votes
2 answers

Extended enum class

I had enum class, say enum class Enum{ var1, var2; } Now I want to add some member which depends on parameter i.e var3(int). OK, It's not for enum, so I want to change it by regular class, but my goal is to leave old code(Enum::var1 as value of…
RiaD
  • 46,822
  • 11
  • 79
  • 123
1
vote
1 answer

operator<< for enum class just doesn't work

I have the following code: logging.hxx #include #include #include #include "singleton.hxx" #include "utils.hxx" class FGaugeLogger: public Singleton { public: enum class LogLevel: int { …
TheEagle
  • 5,808
  • 3
  • 11
  • 39
1
vote
2 answers

Alias template with a switch over template parameters

Consider a alias template declaration, depending on a single template parameter that can have a finite number of values, like a class enum. I would like to a employ using to define a type alias for every value of the class enum. One way of…
francesco
  • 7,189
  • 7
  • 22
  • 49
1
vote
1 answer

Any way to get a warning for static_cast(T) where T's type isn't the underlying type of some_enum_class?

I'm reviewing a lot of code where I need to ensure there are no static_cast (or any cast) calls on variables that could be out of range of the enum class that is being cast to. Ideally I'd be able to receive a warning or have some way to detect code…
GLJeff
  • 139
  • 10
1
vote
3 answers

Avoiding repetitive copy-paste of static_cast(enum_type) for casting an enum class to its underlying type

I have an enum type in my code, like this: enum class GameConsts: size_t { NUM_GHOSTS = 4 }; I find myself repeating the required static_cast to get the enum value: Ghost ghosts[static_cast(GameConsts::NUM_GHOSTS)]; // and... for(size_t i =…
Amir Kirsh
  • 12,564
  • 41
  • 74
1
vote
1 answer

How to query validity of enum class used as bit mask without casting to underlying type

I have an enum class I use for bit masking, like so (in Unreal Engine, therefore the uint8 type) enum class Level : uint8 { None = 0x0, Debug = 0x1, Info = 0x2, Warning = 0x4, ... } I have added inline operators for |, & and ^…
Tare
  • 482
  • 1
  • 9
  • 25
1
vote
1 answer

C-style cast of enum class to reference of underlying type char

Is this legal C++ (>=14), resulting in a char being read and saved into aCode? enum class ECode : char { Code1 = 'a' }; std::istream& operator>>(std::istream& aIn, ECode& aCode) { return aIn >> (std::underlying_type_t&)aCode; } I would…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
1
vote
1 answer

Document enum class values using Doxygen without enabling EXTRACT_ALL

I wasn't able to show documentation for values of enum classes without setting EXTRACT_ALL. The comments for preserve, truncate and append aren't there. The enum itself is documented. If I enable EXTRACT_ALL I get a list. My code is: namespace…
Martin Fehrs
  • 792
  • 4
  • 13
1
vote
2 answers

C++11 How to cast enum class template parameter to int?

I have a struct which takes enum class value on the template parameter. template struct Type { public: static constexpr int VALUE = static_cast(EnumValue); }; enum class MyEnum {Val1, Val2}; std::cout <<…
1
vote
2 answers

I have a enum class in a .h and operator overloading in a .cpp error: no match for ‘operator<<’

OperatingSystem.h #ifndef OPERATING_SYSTEM_H #define OPERATING_SYSTEM_H #include enum class OperatingSystem { unknown, android, iOS, macOS, Linux, propietary, Unix, windows…