Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ enum class - initialization from underlying_type

Tags:

c++

enums

c++11

I want to initialize a strongly-typed enum in C++11 from its underlying type, which is a value I read from serialized stream and which I have to check for a correct number range.

Something like:

enum class MyEnum {
    FOO, BAR
};

MyEnum test = static_cast<MyEnum>(1);

This works as expected, but the problem is:

MyEnum test2 = static_cast<MyEnum>(42);

also works and give no indication of an Error. As far as I see an enum class also does not have any notion of bounds or other indicators on how to check if the input is valid. In "old style" enums, we would include a MIN and MAX value and compare against those, but adding these values to the strongly-typed enum would add invalid values to this type again, undermining its purpose.

Any ideas how I could check the bounds or force an error in case the value is out of bounds?

Update:

I just tried std::numeric_limits, but this also does not work for enum classes:

cout << static_cast<unsigned int>(numeric_limits<MyEnum>::min()) << endl;
cout << static_cast<unsigned int>(numeric_limits<MyEnum>::max()) << endl;

both return 0.

like image 453
VolkA Avatar asked Jun 12 '14 12:06

VolkA


People also ask

Can enum be initialized?

C static code analysis: "enum" members other than the first one should not be explicitly initialized unless all members are explicitly initialized.

How do you declare an enum?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.

Can enum class have methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

How do you use enums in CPP?

It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration. The following is the syntax of enums. enum enum_name{const1, const2, ....... };


1 Answers

There's currently no way to extract an enum's minimum or maximum enumerators, but http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3815.html proposes enough new type traits to implement a library that can do this. The Reflection group was interested, but asked the authors to come back with a proposal that more clearly generalized to other reflection features. I believe http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4027.pdf is the result of that generalization, and it'll get discussed at the meeting this coming week.

like image 147
Jeffrey Yasskin Avatar answered Sep 29 '22 08:09

Jeffrey Yasskin