__ __ _ ______ _____
| \/ | (_) | ____| / ____|_ _
| \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
| |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
| | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
|_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
__/ |
|___/
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
enum_castobtains enum value from string or integer.enum_valuereturns enum value at specified index.enum_valuesobtains enum value sequence.enum_countreturns number of enum values.enum_integerobtains integer value from enum value.enum_namereturns string name from enum value.enum_namesobtains string enum name sequence.enum_entriesobtains pair (value enum, string enum name) sequence.is_unscoped_enumchecks whether type is an Unscoped enumeration.is_scoped_enumchecks whether type is an Scoped enumeration.
- C++17
- Header-only
- Dependency-free
- Compile-time
- Enum to string
- String to enum
- Iterating over enum
// For example color enum.
enum Color { RED = 2, BLUE = 4, GREEN = 8 };-
Enum value to string
Color color = Color::RED; auto color_name = magic_enum::enum_name(color); // color_name -> "RED"
-
String to enum value
std::string color_name{"GREEN"}; auto color = magic_enum::enum_cast<Color>(color_name); if (color.has_value()) { // color.value() -> Color::GREEN } -
Integer to enum value
int color_integer = 2; auto color = magic_enum::enum_cast<Color>(color_integer); if (colo.has_value()) { // color.value() -> Color::RED }
-
Indexed access to enum value
int i = 1; Color colo = magic_enum::enum_value<Color>(i); // color -> Color::BLUE
-
Enum value sequence
constexpr auto colors = magic_enum::enum_values<Color>(); // colors -> {Color::RED, Color::BLUE, Color::GREEN} // colors[0] -> Color::RED
-
Number of enum elements
constexpr std::size_t color_count = magic_enum::enum_count<Color>(); // color_count -> 3
-
Enum value to integer
Color color = Color::RED; auto color_integer = magic_enum::enum_integer(color); // color -> 2
-
Enum names sequence
constexpr auto color_names = magic_enum::enum_names<Color>(); // color_names -> {"RED", "BLUE", "GREEN"} // color_names[0] -> "RED"
-
Enum entries sequence
constexpr auto color_entries = magic_enum::enum_entries<Color>(); // color_entries -> {{Color::RED, "RED"}, {Color::BLUE, "BLUE"}, {Color::GREEN, "GREEN"}} // color_entries[0].first -> Color::RED // color_entries[0].second -> "RED"
-
Stream operator for enum
using namespace magic_enum::ops; // out-of-the-box stream operator for enums. Color color = Color::BLUE; std::cout << color << std::endl; // "BLUE"
-
Checks whether type is an Unscoped enumeration.
enum color { red, green, blue }; enum class direction { left, right }; magic_enum::is_unscoped_enum<color>::value -> true magic_enum::is_unscoped_enum<direction>::value -> false magic_enum::is_unscoped_enum<int>::value -> false // Helper variable template. magic_enum::is_unscoped_enum_v<color> -> true
-
Checks whether type is an Scoped enumeration.
enum color { red, green, blue }; enum class direction { left, right }; magic_enum::is_scoped_enum<color>::value -> false magic_enum::is_scoped_enum<direction>::value -> true magic_enum::is_scoped_enum<int>::value -> false // Helper variable template. magic_enum::is_scoped_enum_v<direction> -> true
-
Static storage enum variable to string
constexpr Color color = Color::BLUE; constexpr auto color_name = magic_enum::enum_name<color>(); // color_name -> "BLUE"
-
magic_enum::enum_cast(value)returnsstd::optional<E>, usinghas_value()to check contains enum value andvalue()to get the enum value. -
magic_enum::enum_value(index)no bounds checking is performed: the behavior is undefined ifindex >= number of enum values. -
magic_enum::enum_values<E>()returnsstd::array<E, N>with all enum value whereN = number of enum values, sorted by enum value. -
magic_enum::enum_name(value)returnsstd::string_view. If enum value does not have name, returns empty string. -
magic_enum::enum_name<value>()is much lighter on the compile times and is not restricted to the enum_range limitation. -
magic_enum::enum_names<E>()returnsstd::array<std::string_view, N>with all string enum name whereN = number of enum values, sorted by enum value. -
magic_enum::enum_entries<E>()returnsstd::array<std::pair<E, std::string_view>, N>with all std::pair (value enum, string enum name) whereN = number of enum values, sorted by enum value. -
Enum value must be in range
[MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By defaultMAGIC_ENUM_RANGE_MIN = -128,MAGIC_ENUM_RANGE_MAX = 128.If need another range for all enum types by default, redefine the macro
MAGIC_ENUM_RANGE_MINandMAGIC_ENUM_RANGE_MAX.#define MAGIC_ENUM_RANGE_MIN 0 #define MAGIC_ENUM_RANGE_MAX 256 #include <magic_enum.hpp>
If need another range for specific enum type, add specialization
enum_rangefor necessary enum type.#include <magic_enum.hpp> enum number { one = 100, two = 200, three = 300 }; namespace magic_enum { template <> struct enum_range<number> { static constexpr int min = 100; static constexpr int max = 300; }; }
-
magic_enumobtains the first defined value enums, and won't work if value are aliased.enum ShapeKind { ConvexBegin = 0, Box = 0, // Won't work. Sphere = 1, ConvexEnd = 2, Donut = 2, // Won't work too. Banana = 3, COUNT = 4, }; // magic_enum::enum_cast<ShapeKind>("Box") -> std::nullopt // magic_enum::enum_name(ShapeKind::Box) -> "ConvexBegin"
Work around the issue:
enum ShapeKind { // Convex shapes, see ConvexBegin and ConvexEnd below. Box = 0, Sphere = 1, // Non-convex shapes. Donut = 2, Banana = 3, COUNT = Banana + 1, // Non-reflected aliases. ConvexBegin = Box, ConvexEnd = Sphere + 1, }; // magic_enum::enum_cast<ShapeKind>("Box") -> ShapeKind::Box // magic_enum::enum_name(ShapeKind::Box) -> "Box" // Non-reflected aliases. // magic_enum::enum_cast<ShapeKind>("ConvexBegin") -> std::nullopt // magic_enum::enum_name(ShapeKind::ConvexBegin) -> "Box"
You should add the required file magic_enum.hpp.
- Clang/LLVM >= 5
- Visual C++ >= 15.3 / Visual Studio >= 2017
- Xcode >= 10.2
- GCC >= 9