mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
add integer_cast
This commit is contained in:
parent
4a540b1a2e
commit
0c1128947b
2 changed files with 37 additions and 0 deletions
|
|
@ -55,6 +55,18 @@ int main() {
|
|||
std::cout << "GREEN = " << c3.value() << std::endl; // GREEN = 10
|
||||
}
|
||||
|
||||
// Enum value to integer value.
|
||||
auto color_underlying = magic_enum::integer_cast(Color::RED);
|
||||
if (color_underlying.has_value() && color_underlying.value() == static_cast<std::underlying_type_t<Color>>(Color::RED)) {
|
||||
std::cout << "RED = " << color_underlying.value() << std::endl; // RED = -10
|
||||
}
|
||||
|
||||
// Enum value to specific type integer value.
|
||||
auto color_int = magic_enum::integer_cast<int>(Color::RED);
|
||||
if (color_int.has_value() && color_int.value() == static_cast<int>(Color::RED)) {
|
||||
std::cout << "RED = " << color_int.value() << std::endl; // RED = -10
|
||||
}
|
||||
|
||||
using namespace magic_enum::ops; // out-of-the-box stream operator for enums.
|
||||
// ostream operator for enum.
|
||||
std::cout << "Color: " << c1 << " " << c2 << " " << c3 << std::endl; // Color: RED BLUE GREEN
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue