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

Using a enum class from a c++ header in a c header

I am writing a c wrapper around a c++ library. In the c++ there are enum classes used as types for function arguments. How do I use theme correctly in the c header. One ugly way would be to use int's in the c function and cast theme in the wrapper…
ludw
  • 113
  • 8
2
votes
2 answers

What are commonly-used ways to iterate over an enum class in C++?

I am finding that all of my standard techniques for iterating over regular enums unfortunately do NOT work on enum classes since enum classes do not implicitly convert to integers. NOT a duplicate of How can I iterate over an enum?, since I'm asking…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
2
votes
4 answers

I want to make a C++ enum of QString formats to display a QTime

I've been working with C++ on a Time class in Qt and I need to write an enum class that contains the formats that I use in showTime(). Because of the nature of the QString formats I've been getting the following error, the value is not convertible…
2
votes
2 answers

Safe and maintainable way to convert int to enum class

I am looking for a safe and maintainable way to convert an int value to enum class. I know i can convert inegral values to enum class by simply using static_cast, but i want to make sure the value to be converted really has a representation in the…
Detonar
  • 1,409
  • 6
  • 18
2
votes
1 answer

Is it possible to mark a enum class member as deprecated?

I've gotten in the habit of using the new enum classes (or strong enums) in C++. Now, I need to deprecate a member. However, the standard enum deprecation syntax, as described in the following question, does not compile. c++ mark enum value as…
Zak
  • 12,213
  • 21
  • 59
  • 105
2
votes
1 answer

Convert enum class to lvalue reference to its underlying type in C++

I have the following code in C++11 (drastically simplified to leave out the inessentials): #include #include enum class State : std::uint8_t { Shutdown, Loading, Active, Idle, Off }; int main() { State state =…
mariia_kornieva
  • 143
  • 1
  • 4
2
votes
2 answers

In a template function, How do I use std::underlying_type just if type of the input is enum class?

I have a piece of code that returns the value of some bits of a given number (I counted enum classes as a number too, using a static_cast). template bool get_bits(Type input, uint8_t offset, uint8_t n, Type* destination) { if…
s4eed
  • 7,173
  • 9
  • 67
  • 104
2
votes
1 answer

Why static_cast enum class that underlying type is int8_t to get an unexpected value?

compiler : clang++ c++ standard : c++20 I tried to run the code, and the results met my expectations very well. #include using namespace std; int main() { enum class Num : int { one = 1, two = 2, …
nullptr
  • 321
  • 1
  • 9
2
votes
2 answers

How should we use an enum class for indexing (or should we better avoid this)?

Say we have an enum type foo which we want to use to index an array arr of static size. If we want to use an enum class for this, we could try it like this: enum class foo { a, b, c, count }; std::array
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
2
votes
1 answer

meaning of code snippet involving enum class and macro expansion

I encounter the following code in C++. I am new to the language and am not familiar with the syntax. I understand the basic of macro expansion and enum class. I learn that the macro etokenize with argument x1,x2,...,xn will be replaced by…
Ryan
  • 219
  • 2
  • 11
2
votes
1 answer

Is there any way to get random from enum class in c++?

I want to fill a variable with random element from enum class. So I tried set enum class type to int and pass last enum from enum class to rand: enum class Enumerator: int { en1=0, en2, en3, ensCount }; int main() { …
LVlad
  • 23
  • 3
2
votes
1 answer

fail proof conversion of string to enum class

I have an enum class like this (I am planning to add more options to it later): enum class ViSequencePointType { JumpToValue = 0, RampToValue = 1 }; Then I have a configuration text file which each line supposed to represents one the enum…
DEKKER
  • 877
  • 6
  • 19
2
votes
2 answers

(De)serializing an enum class

I am trying to serialize and deserialize (using QDataStream but that is irrelevant here) an enum class variable: enum class Type : char { Trivial, Complex }; The serialization is easy: QDataStream &operator<<(QDataStream &stream, Type…
Resurrection
  • 3,916
  • 2
  • 34
  • 56
2
votes
2 answers

How do I check equality of two enum class elements with googletest?

I have an object that has an enum type as a member. enum class Color { Blue=1, Green=2, Red=3}; struct A { int f; Color color; A(int x, Color c) : f(x), color(c) {} }; struct B{ ... std::unique_ptr makeObject(size_t index,…
ssh
  • 369
  • 7
  • 7
2
votes
1 answer

Simple macros to check if enum classes are available

There are a lot of feature test macros in C++ which give a simple and portable way to detect the presence of C++ standards and experimental features. However, I didn't manage to find simple macros to check if enum classes are supported. Is there…
Seleznev Anton
  • 641
  • 5
  • 14