| example | ||
| include | ||
| test | ||
| .appveyor.yml | ||
| .gitignore | ||
| .travis.yml | ||
| CMakeLists.txt | ||
| LICENSE | ||
| README.md | ||
Magic Enum C++
__ __ _ ______ _____
| \/ | (_) | ____| / ____|_ _
| \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
| |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
| | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
|_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
__/ |
|___/
What is Magic Enum?
Header-only C++17 library provides Enum-to-String and String-to-Enum functions.
magic_enum::enum_castobtains enum value from string or integer.magic_enum::enum_valueobtains indexed access to enum value.magic_enum::enum_valuesobtains enum value sequence.magic_enum::enum_countobtains number of enum values.magic_enum::enum_nameobtains string name from enum value.magic_enum::enum_namesobtains string enum name sequence.
Features
- C++17
- Header-only
- Dependency-free
- Compile-time
- Enum to string
- String to enum
- Works with any enum type
Examples
-
Enum value to string
Color color = Color::RED; auto color_name = magic_enum::enum_name(color); if (color_name.has_value()) { // color_name.value() -> "RED" } -
Static storage enum variable to string
constexpr Color color = Color::BLUE; constexpr auto color_name = magic_enum::enum_name(color); if (color_name.has_value()) { // color_name.value() -> "BLUE" } -
String to enum value
constexpr auto color = magic_enum::enum_cast<Color>("GREEN"); if (color.has_value()) { // color.value() -> Color::GREEN } -
Integer to enum value
constexpr auto color = magic_enum::enum_cast<Color>(0); if (color.has_value()) { // color.value() -> Color::RED } -
Indexed access to enum value
constexpr Color color_value = magic_enum::enum_value<Color>(0); // color_element -> Color::RED -
Enum value sequence
constexpr auto colors = magic_enum::enum_values<Color>(); // colors -> {Color::RED, Color::BLUE, Color::GREEN} -
Number of enum elements
constexpr std::size_t color_size = magic_enum::enum_count<Color>(); // color_size -> 3 -
Enum string sequence
constexpr auto color_names = magic_enum::enum_names<Color>(); // color_names -> {"RED", "BLUE", "GREEN"} -
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"
Remarks
-
magic_enum::enum_castreturnsstd::optional<E>, usinghas_value()to check contains enum value andvalue()to get the enum value. -
magic_enum::enum_valuesreturnsstd::array<E, N>with all enum value, sorted by enum value. -
magic_enum::enum_namereturnsstd::optional<std::string_view>, usinghas_value()to check contains enum name andvalue()to get the enum name. -
magic_enum::enum_namesreturnsstd::array<std::string_view, N>with all string enum name, sorted by enum value. -
Enum value must be in range
(-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE). By defaultMAGIC_ENUM_RANGE = 128. If you need larger range, redefine the macroMAGIC_ENUM_RANGE.#define MAGIC_ENUM_RANGE 1028 // Redefine MAGIC_ENUM_RANGE for larger range. #include <magic_enum.hpp>
Integration
You should add the required file magic_enum.hpp.
Compiler compatibility
- Clang/LLVM >= 5
- Visual C++ >= 15.3 / Visual Studio >= 2017
- Xcode >= 10.2
- GCC >= 9