MCPcopy Create free account
hub / github.com/Neargye/magic_enum / main

Function main

example/example.cpp:36–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34}
35
36int main() {
37 // Enum variable to string name.
38 Color c1 = Color::RED;
39 auto c1_name = magic_enum::enum_name(c1);
40 std::cout << c1_name << std::endl; // RED
41
42 // String enum name sequence.
43 constexpr auto names = magic_enum::enum_names<Color>();
44 std::cout << "Color names:";
45 for (const auto& n : names) {
46 std::cout << " " << n;
47 }
48 std::cout << std::endl;
49 // Color names: RED BLUE GREEN
50
51 // String name to enum value.
52 auto c2 = magic_enum::enum_cast<Color>("BLUE");
53 if (c2.has_value()) {
54 std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
55 }
56
57 // Case insensitive enum_cast.
58 c2 = magic_enum::enum_cast<Color>("blue", magic_enum::case_insensitive);
59 if (c2.has_value()) {
60 std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
61 }
62
63 // Integer value to enum value.
64 auto c3 = magic_enum::enum_cast<Color>(10);
65 if (c3.has_value()) {
66 std::cout << "GREEN = " << magic_enum::enum_integer(c3.value()) << std::endl; // GREEN = 10
67 }
68
69 // Enum value to integer value.
70 auto c4_integer = magic_enum::enum_integer(Color::RED);
71 std::cout << "RED = " << c4_integer << std::endl; // RED = -10
72
73 using magic_enum::iostream_operators::operator<<; // out-of-the-box ostream operator for all enums.
74 // Ostream operator for enum.
75 std::cout << "Color: " << c1 << " " << c2 << " " << c3 << std::endl; // Color: RED BLUE GREEN
76
77 // Number of enum values.
78 std::cout << "Color enum size: " << magic_enum::enum_count<Color>() << std::endl; // Color size: 3
79
80 // Indexed access to enum value.
81 std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
82
83 // Enum value sequence.
84 constexpr auto values = magic_enum::enum_values<Color>();
85 std::cout << "Colors values:";
86 for (const auto c : values) {
87 std::cout << " " << c; // Ostream operator for enum.
88 }
89 std::cout << std::endl;
90 // Color values: RED BLUE GREEN
91
92 enum class Flags { A = 1, B = 2, C = 4, D = 8 };
93 using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for all enums.

Callers

nothing calls this directly

Calls 5

enum_nameFunction · 0.85
to_integerFunction · 0.85
enum_integerFunction · 0.85
has_valueMethod · 0.80
valueMethod · 0.45

Tested by

no test coverage detected