![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Dependant Packages
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Dependant Packages
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Dependant Packages
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from yaml_cpp_struct repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.0.1 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | It's easy to mapping yaml to cpp's struct |
Checkout URI | https://github.com/fantasy-peak/yaml_cpp_struct.git |
VCS Type | git |
VCS Version | main |
Last Updated | 2024-04-29 |
Dev Status | UNKNOWN |
Released | UNRELEASED |
Tags | yaml cpp mapping cpp11 header-only cpp17 |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- fantasy-peak
Authors
yaml_cpp_struct
function
It’s a wrapper, It’s easy to mapping yaml to cpp’s struct
note
main branch need >= c++17, if you need to use c++11, please use cpp11 branch. cpp11 branch not support std::variant. Please refer to example for cpp11 branch
use examples
// It relies on two other three open source libraries(yaml-cpp, visit_struct, magic_enum), Please refer to CMakeLists.txt of example
git clone https://github.com/fantasy-peak/yaml_cpp_struct.git
cd ./yaml_cpp_struct/example
mkdir build && cd build
cmake ..
make -j 9
./example ../config.yaml
// use xmake
cd ./yaml_cpp_struct/example
xmake build -v
xmake run example ../../../../config.yaml
---
ch: 'A'
price: 100.05
count: 254
content: "i'm fantasy-peak"
map: { "fantasy": "good", "peak": "good" }
account_info:
flag: false
name: "fantasy-peak"
address: { "127.0.0.1": 8888, "127.0.0.2": 9999 }
msec: 100
tuple: ["fantasy-peak", 89]
map_tuple: {"fantasy-peak": ["map_tuple", 254]}
vec: ["fantasy-001", "fantasy-002"]
set_vec: [9, 5, 7]
account_type: "Personal"
ips: ["127.0.0.1"]
#include <iostream>
#include <spdlog/spdlog.h>
#include <yaml_cpp_struct.hpp>
enum class AccountType : uint8_t {
Personal = 1,
Company = 2,
};
YCS_ADD_ENUM(AccountType, Personal, Company)
std::string to_string(const AccountType& type) {
if (type == AccountType::Personal)
return "Personal";
if (type == AccountType::Company)
return "Company";
throw std::bad_cast();
}
struct AccountInfo {
bool flag;
std::string name;
std::unordered_map<std::string, int> address;
std::optional<std::string> num;
std::chrono::milliseconds msec{100};
std::tuple<std::string, uint8_t> tuple;
std::unordered_map<std::string, std::tuple<std::string, int32_t>> map_tuple;
std::string default_str{"hello default"};
std::vector<std::string> ips;
};
YCS_ADD_STRUCT(AccountInfo, flag, name, address, num, msec, tuple, map_tuple, default_str, ips)
int main(int, char** argv) {
auto [cfg, error] = yaml_cpp_struct::from_yaml<Config>(argv[1]);
auto [str, e] = yaml_cpp_struct::to_yaml(cfg.value());
// Load values from environment variables
// export YCS_ENV_IPS=["Google","Runoob","Taobao"]
// auto [cfg, error] = yaml_cpp_struct::from_yaml_env<Config>(argv[1], "YCS_ENV_");
return 0;
}
Dependant Packages
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Dependant Packages
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Dependant Packages
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Launch files
Messages
Services
Plugins
Recent questions tagged magic_enum at Robotics Stack Exchange
![]() |
magic_enum package from magic_enum repomagic_enum |
ROS Distro
|
Package Summary
Tags | No category tags. |
Version | 0.9.7 |
License | MIT |
Build type | CMAKE |
Use | RECOMMENDED |
Repository Summary
Description | Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code |
Checkout URI | https://github.com/Neargye/magic_enum.git |
VCS Type | git |
VCS Version | master |
Last Updated | 2025-07-17 |
Dev Status | MAINTAINED |
Released | RELEASED |
Tags | c-plus-plus serialization reflection cplusplus cpp metaprogramming enum header-only cpp17 c-plus-plus-17 no-dependencies single-file cplusplus-17 enum-to-string string-to-enum |
Contributing |
Help Wanted (-)
Good First Issues (-) Pull Requests to Review (-) |
Package Description
Additional Links
Maintainers
- Neargye
Authors
Magic Enum C++
Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
Documentation
Features & Examples
- Basic
#include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
- 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
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
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
File truncated at 100 lines see the full file
Dependant Packages
Name | Deps |
---|---|
movie_publisher |