Please don't do this. If you need to do this, enum class
s probably aren't what you need.
@T.C. showed you how to do it so long as you specify underlying type, but you will run into places where your program does things it just shouldn't.
An example is where you use a switch
and have a case
for every defined enum value.
e.g.
enum class my_enum: unsigned int{
first = 1,
second = 2,
third = 4,
fourth = 8
};
int main(){
auto e = static_cast<my_enum>(static_cast<unsigned int>(my_enum::first) | static_cast<unsigned int>(my_enum::second));
switch(e){
case my_enum::first:
case my_enum::second:
case my_enum::third:
case my_enum::fourth:
return 0;
}
std::cout << "Oh, no! You reached a part of the program you weren't meant to!\n";
return 1;
}
Will output:
Oh, no! You reached a part of the program you weren't meant to!
then return the error code 1
.
Which is also an example of why you should always have a default
case, of course, but that isn't my point.
Of course, you could argue that so long as the user of the enum class
never directly uses the value other than passing to a function; it would be a good way of restricting the values of a bitset. But I've always been a little too trustworthy and find std::uint[n]_t
and some constexpr
variables the best way (if a user sets an invalid bit it simply does nothing).
What you're doing just isn't really suitable for enum class
, because it defeats the purpose of having a scoped enumeration. You can no longer enumerate the values if you set it to an undefined one.