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.
enum_cast
obtains enum value from string or integer.enum_value
returns enum value at specified index.enum_values
obtains enum value sequence.enum_count
returns number of enum values.enum_integer
obtains integer value from enum value.enum_name
returns name from enum value.enum_names
obtains string enum name sequence.enum_entries
obtains pair (value enum, string enum name) sequence.enum_index
obtains index in enum value sequence from enum value.enum_contains
checks whether enum contains enumerator with such value.enum_type_name
returns name of enum type.is_unscoped_enum
checks whether type is an Unscoped enumeration.is_scoped_enum
checks whether type is an Scoped enumeration.underlying_type
improved UB-free "SFINAE-friendly" underlying_type.ostream_operators
ostream operators for enums.bitwise_operators
bitwise operators for enums.
Documentation
Features
- C++17
- Header-only
- Dependency-free
- Compile-time
- Enum to string
- String to enum
- Iterating over enum
Examples
// For example color enum.
enum class 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 (color.has_value()) { // color.value() -> Color::RED }
-
Indexed access to enum value
std::size_t i = 1; Color color = 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"
-
Ostream 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"
-
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"
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.
-
For the small enum use the API from the namespace
magic_enum
, and for enum-flags use the API from the namespacemagic_enum::flags
.
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>
.)
Compiler compatibility
- Clang/LLVM >= 5
- MSVC++ >= 14.11 / Visual Studio >= 2017
- Xcode >= 10
- GCC >= 9
- MinGW >= 9
Licensed under the MIT License
version | 0.7.3+1 |
---|---|
license | MIT License |
repository | https://pkg.cppget.org/1/alpha |
download | magic_enum-0.7.3+1.tar.gz |
sha256 | c57cd73ce94d9e06dfde9f50a98c0f773647636a79306c6f91cc2043eb3a266d |
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@protonmail.com |
topics | Enumreflection |
Requires (1)
c++ >= 17 |
Tests
magic_enum-tests == 0.7.3 |
Builds
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13-static_O3 |
timestamp | 2024-04-18 22:39:01 UTC (15:57:09 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13-ndebug_O3 |
timestamp | 2024-04-18 22:38:34 UTC (15:57:35 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13-O3 |
timestamp | 2024-04-18 22:38:13 UTC (15:57:57 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13 |
timestamp | 2024-04-18 22:37:49 UTC (15:58:21 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_14.0-static_O3 |
timestamp | 2024-04-18 15:04:02 UTC (23:32:07 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_14.0-O3 |
timestamp | 2024-04-18 15:03:17 UTC (23:32:53 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_14.0 |
timestamp | 2024-04-18 15:02:34 UTC (23:33:36 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_15_libc++ |
timestamp | 2024-04-18 08:42:57 UTC (01 05:53:13 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_15 |
timestamp | 2024-04-18 08:40:50 UTC (01 05:55:19 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_11-gcc_12 |
timestamp | 2024-04-18 08:39:37 UTC (01 05:56:32 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16_libc++-static_O3 |
timestamp | 2024-04-18 06:40:24 UTC (01 07:55:46 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16_libc++-O3 |
timestamp | 2024-04-18 06:40:14 UTC (01 07:55:56 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16_libc++ |
timestamp | 2024-04-18 06:39:40 UTC (01 07:56:29 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16-static_O3 |
timestamp | 2024-04-18 06:39:34 UTC (01 07:56:35 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16-O3 |
timestamp | 2024-04-18 06:38:58 UTC (01 07:57:12 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16 |
timestamp | 2024-04-18 06:38:53 UTC (01 07:57:16 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_38-gcc_13-bindist |
timestamp | 2024-04-17 08:58:43 UTC (02 05:37:26 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-gcc_13_homebrew-static_O3 |
timestamp | 2024-04-17 03:52:15 UTC (02 10:43:54 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-gcc_13_homebrew-O3 |
timestamp | 2024-04-17 03:51:27 UTC (02 10:44:43 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-gcc_13_homebrew |
timestamp | 2024-04-17 03:50:31 UTC (02 10:45:38 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_37-gcc_12.2-bindist |
timestamp | 2024-04-17 03:31:40 UTC (02 11:04:29 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0-static_O3 |
timestamp | 2024-04-17 01:26:25 UTC (02 13:09:45 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0-O3 |
timestamp | 2024-04-17 01:24:16 UTC (02 13:11:53 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0 |
timestamp | 2024-04-17 01:22:09 UTC (02 13:14:01 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_12.2_mingw_w64-static_O2 |
timestamp | 2024-04-16 23:52:52 UTC (02 14:43:17 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_12.2_mingw_w64-O2 |
timestamp | 2024-04-16 23:52:18 UTC (02 14:43:52 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_12.2_mingw_w64 |
timestamp | 2024-04-16 23:51:31 UTC (02 14:44:39 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_16.0_llvm_msvc_17.6-static_O2 |
timestamp | 2024-04-16 23:48:58 UTC (02 14:47:11 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.6-O2 |
timestamp | 2024-04-16 23:48:56 UTC (02 14:47:14 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.6-static_O2 |
timestamp | 2024-04-16 23:48:33 UTC (02 14:47:37 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_16.0_llvm_msvc_17.6-O2 |
timestamp | 2024-04-16 23:48:13 UTC (02 14:47:57 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_16.0_llvm_msvc_17.6 |
timestamp | 2024-04-16 23:48:09 UTC (02 14:48:01 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.5 |
timestamp | 2024-04-16 23:48:00 UTC (02 14:48:10 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_15.0_msvc_msvc_17.6 |
timestamp | 2024-04-16 23:47:43 UTC (02 14:48:26 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.6 |
timestamp | 2024-04-16 23:47:27 UTC (02 14:48:43 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd13.2 |
tgt config | freebsd_13-clang_14.0-O3 |
timestamp | 2024-04-16 23:44:18 UTC (02 14:51:51 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd13.2 |
tgt config | freebsd_13-clang_14.0-static_O3 |
timestamp | 2024-04-16 23:44:00 UTC (02 14:52:10 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1-ndebug_O3 |
timestamp | 2024-04-16 23:43:52 UTC (02 14:52:18 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0_libc++-static_O3 |
timestamp | 2024-04-16 23:43:31 UTC (02 14:52:39 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1-static_O3 |
timestamp | 2024-04-16 23:43:13 UTC (02 14:52:57 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd12.4 |
tgt config | freebsd_12-clang_13.0 |
timestamp | 2024-04-16 23:43:05 UTC (02 14:53:05 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0_libc++-O3 |
timestamp | 2024-04-16 23:43:03 UTC (02 14:53:07 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd13.2 |
tgt config | freebsd_13-clang_14.0 |
timestamp | 2024-04-16 23:42:54 UTC (02 14:53:15 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0-static_O3 |
timestamp | 2024-04-16 23:42:33 UTC (02 14:53:37 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1-O3 |
timestamp | 2024-04-16 23:42:32 UTC (02 14:53:38 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_12-bindist |
timestamp | 2024-04-16 23:42:20 UTC (02 14:53:50 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0_libc++ |
timestamp | 2024-04-16 23:42:12 UTC (02 14:53:58 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0-O3 |
timestamp | 2024-04-16 23:41:36 UTC (02 14:54:34 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_15.0_libc++ |
timestamp | 2024-04-16 23:41:15 UTC (02 14:54:55 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0 |
timestamp | 2024-04-16 23:40:55 UTC (02 14:55:15 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_15.0 |
timestamp | 2024-04-16 23:40:45 UTC (02 14:55:24 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1 |
timestamp | 2024-04-16 23:40:02 UTC (02 14:56:07 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_11-gcc_12.1 |
timestamp | 2024-04-16 23:39:28 UTC (02 14:56:41 days ago) |
result | success | log | rebuild |