-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathenum_to_underlying.cpp
More file actions
33 lines (28 loc) · 906 Bytes
/
enum_to_underlying.cpp
File metadata and controls
33 lines (28 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
enum class Fruit{
APPLE = 0,
BANANA,
COCONUT,
DURIAN
};
enum class Vehicle : uint8_t {
CAR = 0,
AIRPLANE,
SHIP
};
template <typename E>
constexpr typename std::underlying_type<E>::type to_underlying(E e) noexcept {
return static_cast<typename std::underlying_type<E>::type>(e);
};
int main() {
std::cout << "Fruit::COCONUT's underlying value: " << to_underlying(Fruit::COCONUT)
<< ", underlying type: " << typeid(to_underlying(Fruit::COCONUT)).name() << std::endl;
std::cout << "Vehicle::AIRPLANE's underlying value: " << to_underlying(Vehicle::AIRPLANE)
<< ", underlying type: " << typeid(to_underlying(Vehicle::AIRPLANE)).name() << std::endl;
//Fruit::COCONUT's underlying value: 2
return 0;
}
/**
Fruit::COCONUT's underlying value: 2, underlying type: i
Vehicle::AIRPLANE's underlying value: , underlying type: h
**/