Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
__ __ _ ______ _____
| \/ | (_) | ____| / ____|_ _
| \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
| |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
| | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
|_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
__/ |
|___/
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
Documentation
Examples
-
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 } auto color_or_default = magic_enum::enum_cast<Color>(value).value_or(Color::NONE);
-
Integer to enum value
int color_integer = 2; auto color = magic_enum::enum_cast<Color>(color_integer); if (color.has_value()) { // color.value() -> Color::BLUE } auto color_or_default = magic_enum::enum_cast<Color>(value).value_or(Color::NONE);
-
Indexed access to enum value
std::size_t i = 0; Color color = magic_enum::enum_value<Color>(i); // color -> Color::RED
-
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); // or magic_enum::enum_underlying(color); // color_integer -> 1
-
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"
-
Enum fusion for multi-level switch/case statements
switch (magic_enum::enum_fuse(color, direction).value()) { case magic_enum::enum_fuse(Color::RED, Directions::Up).value(): // ... case magic_enum::enum_fuse(Color::BLUE, Directions::Down).value(): // ... // ... }
-
Enum switch runtime value as constexpr constant
Color color = Color::RED; magic_enum::enum_switch([] (auto val) { constexpr Color c_color = val; // ... }, color);
-
Enum iterate for each enum as constexpr constant
magic_enum::enum_for_each<Color>([] (auto val) { constexpr Color c_color = val; // ... });
-
Check if enum contains
magic_enum::enum_contains(Color::GREEN); // -> true magic_enum::enum_contains<Color>(2); // -> true magic_enum::enum_contains<Color>(123); // -> false magic_enum::enum_contains<Color>("GREEN"); // -> true magic_enum::enum_contains<Color>("fda"); // -> false
-
Enum index in sequence
constexpr auto color_index = magic_enum::enum_index(Color::BLUE); // color_index.value() -> 1 // color_index.has_value() -> true
-
Functions for flags
enum Directions : std::uint64_t { Left = 1, Down = 2, Up = 4, Right = 8, }; template <> struct magic_enum::customize::enum_range<Directions> { static constexpr bool is_flags = true; }; magic_enum::enum_flags_name(Directions::Up | Directions::Right); // directions_name -> "Directions::Up|Directions::Right" magic_enum::enum_flags_contains(Directions::Up | Directions::Right); // -> true magic_enum::enum_flags_cast(3); // -> "Directions::Left|Directions::Down"
-
Enum type name
Color color = Color::RED; auto type_name = magic_enum::enum_type_name<decltype(color)>(); // type_name -> "Color"
-
IOstream operator for enum
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operators for enums. Color color = Color::BLUE; std::cout << color << std::endl; // "BLUE" using namespace magic_enum::ostream_operators; // out-of-the-box ostream operators for enums. Color color = Color::BLUE; std::cout << color << std::endl; // "BLUE"
-
Bitwise operator for enum
enum class Flags { A = 1 << 0, B = 1 << 1, C = 1 << 2, D = 1 << 3 }; using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for enums. // Support operators: ~, |, &, ^, |=, &=, ^=. Flags flags = Flags::A | Flags::B & ~Flags::C;
-
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 This version is much lighter on the compile times and is not restricted to the enum_range limitation.
constexpr Color color = Color::BLUE; constexpr auto color_name = magic_enum::enum_name<color>(); // color_name -> "BLUE"
-
containers::array
array container for enums.magic_enum::containers::array<Color, RGB> color_rgb_array {}; color_rgb_array[Color::RED] = {255, 0, 0}; color_rgb_array[Color::GREEN] = {0, 255, 0}; color_rgb_array[Color::BLUE] = {0, 0, 255}; std::get<Color::BLUE>(color_rgb_array) // -> RGB{0, 0, 255}
-
containers::bitset
bitset container for enums.constexpr magic_enum::containers::bitset<Color> color_bitset_red_green {Color::RED|Color::GREEN}; bool all = color_bitset_red_green.all(); // all -> false // Color::BLUE is missing bool test = color_bitset_red_green.test(Color::RED); // test -> true
-
containers::set
set container for enums.auto color_set = magic_enum::containers::set<Color>(); bool empty = color_set.empty(); // empty -> true color_set.insert(Color::GREEN); color_set.insert(Color::BLUE); color_set.insert(Color::RED); std::size_t size = color_set.size(); // size -> 3
-
Improved UB-free "SFINAE-friendly" underlying_type.
magic_enum::underlying_type<color>::type -> int // Helper types. magic_enum::underlying_type_t<Direction> -> int
Remarks
-
magic_enum
does not pretend to be a silver bullet for reflection for enums, it was originally designed for small enum. -
Before use, read the limitations of functionality.
Integration
-
You should add the required file magic_enum.hpp.
-
If you are using vcpkg on your project for external dependencies, then you can use the magic-enum package.
-
If you are using Conan to manage your dependencies, merely add
magic_enum/x.y.z
to your conan's requires, wherex.y.z
is the release version you want to use. -
If you are using Build2 to build and manage your dependencies, add
depends: magic_enum ^x.y.z
to the manifest file wherex.y.z
is the release version you want to use. You can then import the target usingmagic_enum%lib{magic_enum}
. -
Alternatively, you can use something like CPM which is based on CMake's
Fetch_Content
module.CPMAddPackage( NAME magic_enum GITHUB_REPOSITORY Neargye/magic_enum GIT_TAG x.y.z # Where `x.y.z` is the release version you want to use. )
-
Bazel is also supported, simply add to your WORKSPACE file:
http_archive( name = "magic_enum", strip_prefix = "magic_enum-<commit>", urls = ["https://github.com/Neargye/magic_enum/archive/<commit>.zip"], )
To use bazel inside the repository it's possible to do:
bazel build //... bazel test //... bazel run //example
(Note that you must use a supported compiler or specify it with
export CC= <compiler>
.) -
If you are using Ros, you can include this package by adding
<depend>magic_enum</depend>
to your package.xml and include this package in your workspace. In your CMakeLists.txt add the following:find_package(magic_enum CONFIG REQUIRED) ... target_link_libraries(your_executable magic_enum::magic_enum)
Compiler compatibility
- Clang/LLVM >= 5
- MSVC++ >= 14.11 / Visual Studio >= 2017
- Xcode >= 10
- GCC >= 9
- MinGW >= 9
Licensed under the MIT License
version | 0.9.1 |
---|---|
license | MIT License |
repository | https://pkg.cppget.org/1/alpha |
download | magic_enum-0.9.1.tar.gz |
sha256 | 5de0e8540d83b8d398be70d9a4b969b275b23f4ae86efe72965c01730f66af0f |
project | magic_enum |
---|---|
doc-url | github.com/Neargye/magic_enum/blob/master/doc/reference.md |
src-url | github.com/Neargye/magic_enum |
package-email | wmbat-dev@protonmail.com |
topics | Enumreflection |
Requires (1)
c++ >= 17 |
Tests
magic_enum-tests == 0.9.1 |
Reviews
fail | 0 |
---|---|
pass | 1 |
Builds
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-09-17 14:52:45 UTC (04:52:49 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17 |
timestamp | 2025-09-17 14:50:58 UTC (04:54:36 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13 |
timestamp | 2025-09-17 13:51:18 UTC (05:54:16 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10-static_O2 |
timestamp | 2025-09-16 21:53:51 UTC (21:51:43 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10-O2 |
timestamp | 2025-09-16 21:51:41 UTC (21:53:53 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10 |
timestamp | 2025-09-16 21:48:58 UTC (21:56:36 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_ubuntu_24.04-gcc_13-bindist |
timestamp | 2025-09-16 21:18:59 UTC (22:26:34 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10 |
timestamp | 2025-09-16 21:11:51 UTC (22:33:43 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64 |
timestamp | 2025-09-16 21:11:28 UTC (22:34:05 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64-O2 |
timestamp | 2025-09-16 21:11:17 UTC (22:34:16 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64-static_O2 |
timestamp | 2025-09-16 21:10:29 UTC (22:35:05 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10-static_O2 |
timestamp | 2025-09-16 21:09:28 UTC (22:36:06 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10-O2 |
timestamp | 2025-09-16 21:08:22 UTC (22:37:12 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8-static_O2 |
timestamp | 2025-09-16 21:08:04 UTC (22:37:30 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8-O2 |
timestamp | 2025-09-16 21:05:50 UTC (22:39:44 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8 |
timestamp | 2025-09-16 21:04:25 UTC (22:41:08 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_17_msvc_msvc_17.10 |
timestamp | 2025-09-16 21:00:55 UTC (22:44:39 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_12-bindist |
timestamp | 2025-09-16 20:55:38 UTC (22:49:56 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd13.3 |
tgt config | freebsd_13-clang_17 |
timestamp | 2025-09-16 20:43:57 UTC (23:01:37 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-ndebug_O3 |
timestamp | 2025-09-16 20:43:55 UTC (23:01:39 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-O3 |
timestamp | 2025-09-16 20:43:49 UTC (23:01:45 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-static_O3 |
timestamp | 2025-09-16 20:42:37 UTC (23:02:57 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14 |
timestamp | 2025-09-16 20:42:31 UTC (23:03:03 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18-O3 |
timestamp | 2025-09-16 20:35:56 UTC (23:09:38 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18-static_O3 |
timestamp | 2025-09-16 20:35:55 UTC (23:09:39 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18 |
timestamp | 2025-09-16 20:33:07 UTC (23:12:27 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-09-16 20:30:42 UTC (23:14:52 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1 |
timestamp | 2025-09-16 20:30:35 UTC (23:14:59 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_17 |
timestamp | 2025-09-16 20:29:55 UTC (23:15:39 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-static_O3 |
timestamp | 2025-09-16 12:34:05 UTC (01 07:11:29 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-ndebug_O3 |
timestamp | 2025-09-16 12:33:24 UTC (01 07:12:09 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-O3 |
timestamp | 2025-09-16 12:29:20 UTC (01 07:16:14 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14 |
timestamp | 2025-09-16 12:28:39 UTC (01 07:16:54 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-static_O3 |
timestamp | 2025-09-15 23:01:16 UTC (01 20:44:17 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-O3 |
timestamp | 2025-09-15 22:48:33 UTC (01 20:57:01 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_40-gcc_14-bindist |
timestamp | 2025-09-15 05:32:14 UTC (02 14:13:19 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++ |
timestamp | 2025-09-15 04:30:42 UTC (02 15:14:52 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18 |
timestamp | 2025-09-15 04:29:59 UTC (02 15:15:35 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18-static_O3 |
timestamp | 2025-09-15 04:29:57 UTC (02 15:15:36 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18-O3 |
timestamp | 2025-09-15 04:28:13 UTC (02 15:17:21 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++ |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_39-gcc_13-bindist |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew-static_O3 |
result | unbuilt |