C++11- enum class with underlying type

less than 1 minute read

C++11 allows us to specify the underlying type for enum class as shown below. If specified value is not in the range of the underlying type, compiler can catch error as well.

1
2
3
4
5
6
enum class Color : unsigned char
{
    Red = 0x01,
    Green = 0x02,
    Blue = 0x04
}

Getting underlying type from enum type

1
std::underlying_type<Color>::type value;