{"id":13494585,"url":"https://github.com/Neargye/magic_enum","last_synced_at":"2025-03-28T14:31:24.451Z","repository":{"id":37413127,"uuid":"178613040","full_name":"Neargye/magic_enum","owner":"Neargye","description":"Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code","archived":false,"fork":false,"pushed_at":"2025-03-01T06:41:02.000Z","size":716,"stargazers_count":5269,"open_issues_count":22,"forks_count":467,"subscribers_count":66,"default_branch":"master","last_synced_at":"2025-03-25T20:01:13.099Z","etag":null,"topics":["c-plus-plus","c-plus-plus-17","cplusplus","cplusplus-17","cpp","cpp17","enum","enum-to-string","header-only","metaprogramming","no-dependencies","reflection","serialization","single-file","string-to-enum"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Neargye.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["paypal.me/Neargye","btc.com/btc/address/bc1qzevldln8tqz5xf4lyufu9msgl7t97xstth9zq8","etherscan.io/address/0xbb42fdef9204fa6f3d535d202b088dee35fcbd31"],"liberapay":"neargye"}},"created_at":"2019-03-30T21:25:29.000Z","updated_at":"2025-03-25T16:29:52.000Z","dependencies_parsed_at":"2023-12-16T13:57:47.576Z","dependency_job_id":"1d2ebf40-5a81-4fe8-8ac9-1c7fb1e8b80b","html_url":"https://github.com/Neargye/magic_enum","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fmagic_enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fmagic_enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fmagic_enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fmagic_enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Neargye","download_url":"https://codeload.github.com/Neargye/magic_enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246045959,"owners_count":20714877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-plus-plus","c-plus-plus-17","cplusplus","cplusplus-17","cpp","cpp17","enum","enum-to-string","header-only","metaprogramming","no-dependencies","reflection","serialization","single-file","string-to-enum"],"created_at":"2024-07-31T19:01:26.308Z","updated_at":"2025-03-28T14:31:24.436Z","avatar_url":"https://github.com/Neargye.png","language":"C++","readme":"[![Github releases](https://img.shields.io/github/release/Neargye/magic_enum.svg)](https://github.com/Neargye/magic_enum/releases)\n[![Conan package](https://img.shields.io/badge/Conan-package-blueviolet)](https://conan.io/center/recipes/magic_enum)\n[![Vcpkg package](https://img.shields.io/badge/Vcpkg-package-blueviolet)](https://github.com/microsoft/vcpkg/tree/master/ports/magic-enum)\n[![Build2 package](https://img.shields.io/badge/Build2-package-blueviolet)](https://www.cppget.org/magic_enum?q=magic_enum)\n[![Meson wrap](https://img.shields.io/badge/Meson-wrap-blueviolet)](https://github.com/mesonbuild/wrapdb/blob/master/subprojects/magic_enum.wrap)\n[![License](https://img.shields.io/github/license/Neargye/magic_enum.svg)](LICENSE)\n[![Compiler explorer](https://img.shields.io/badge/compiler_explorer-online-blue.svg)](https://godbolt.org/z/feqcPa5G6)\n[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/Neargye/magic_enum/badge)](https://securityscorecards.dev/viewer/?uri=github.com/Neargye/magic_enum)\n[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg)](https://stand-with-ukraine.pp.ua)\n\n# Magic Enum C++\n\nHeader-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.\n\nIf you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.\n\n## Documentation\n\n* [Reference](doc/reference.md)\n* [Limitations](doc/limitations.md)\n* [Integration](#Integration)\n\n## [Features \u0026 Examples](example/)\n\n* Basic\n\n  ```cpp\n  #include \u003cmagic_enum/magic_enum.hpp\u003e\n  #include \u003ciostream\u003e\n\n  enum class Color : { RED = -10, BLUE = 0, GREEN = 10 };\n\n  int main() {\n    Color c1 = Color::RED;\n    std::cout \u003c\u003c magic_enum::enum_name(c1) \u003c\u003c std::endl; // RED\n    return 0;\n  }\n  ```\n\n* Enum value to string\n\n  ```cpp\n  Color color = Color::RED;\n  auto color_name = magic_enum::enum_name(color);\n  // color_name -\u003e \"RED\"\n  ```\n\n* String to enum value\n\n  ```cpp\n  std::string color_name{\"GREEN\"};\n  auto color = magic_enum::enum_cast\u003cColor\u003e(color_name);\n  if (color.has_value()) {\n    // color.value() -\u003e Color::GREEN\n  }\n\n  // case insensitive enum_cast\n  auto color = magic_enum::enum_cast\u003cColor\u003e(value, magic_enum::case_insensitive);\n\n  // enum_cast with BinaryPredicate\n  auto color = magic_enum::enum_cast\u003cColor\u003e(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }\n\n  // enum_cast with default\n  auto color_or_default = magic_enum::enum_cast\u003cColor\u003e(value).value_or(Color::NONE);\n  ```\n\n* Integer to enum value\n\n  ```cpp\n  int color_integer = 2;\n  auto color = magic_enum::enum_cast\u003cColor\u003e(color_integer);\n  if (color.has_value()) {\n    // color.value() -\u003e Color::BLUE\n  }\n\n  auto color_or_default = magic_enum::enum_cast\u003cColor\u003e(value).value_or(Color::NONE);\n  ```\n\n* Indexed access to enum value\n\n  ```cpp\n  std::size_t i = 0;\n  Color color = magic_enum::enum_value\u003cColor\u003e(i);\n  // color -\u003e Color::RED\n  ```\n\n* Enum value sequence\n\n  ```cpp\n  constexpr auto colors = magic_enum::enum_values\u003cColor\u003e();\n  // colors -\u003e {Color::RED, Color::BLUE, Color::GREEN}\n  // colors[0] -\u003e Color::RED\n  ```\n\n* Number of enum elements\n\n  ```cpp\n  constexpr std::size_t color_count = magic_enum::enum_count\u003cColor\u003e();\n  // color_count -\u003e 3\n  ```\n\n* Enum value to integer\n\n  ```cpp\n  Color color = Color::RED;\n  auto color_integer = magic_enum::enum_integer(color); // or magic_enum::enum_underlying(color);\n  // color_integer -\u003e 1\n  ```\n\n* Enum names sequence\n\n  ```cpp\n  constexpr auto color_names = magic_enum::enum_names\u003cColor\u003e();\n  // color_names -\u003e {\"RED\", \"BLUE\", \"GREEN\"}\n  // color_names[0] -\u003e \"RED\"\n  ```\n\n* Enum entries sequence\n\n  ```cpp\n  constexpr auto color_entries = magic_enum::enum_entries\u003cColor\u003e();\n  // color_entries -\u003e {{Color::RED, \"RED\"}, {Color::BLUE, \"BLUE\"}, {Color::GREEN, \"GREEN\"}}\n  // color_entries[0].first -\u003e Color::RED\n  // color_entries[0].second -\u003e \"RED\"\n  ```\n\n* Enum fusion for multi-level switch/case statements\n\n  ```cpp\n  switch (magic_enum::enum_fuse(color, direction).value()) {\n    case magic_enum::enum_fuse(Color::RED, Directions::Up).value(): // ...\n    case magic_enum::enum_fuse(Color::BLUE, Directions::Down).value(): // ...\n  // ...\n  }\n  ```\n\n* Enum switch runtime value as constexpr constant\n  ```cpp\n  Color color = Color::RED;\n  magic_enum::enum_switch([] (auto val) {\n    constexpr Color c_color = val;\n    // ...\n  }, color);\n  ```\n\n* Enum iterate for each enum as constexpr constant\n  ```cpp\n  magic_enum::enum_for_each\u003cColor\u003e([] (auto val) {\n    constexpr Color c_color = val;\n    // ...\n  });\n  ```\n\n* Check if enum contains\n\n  ```cpp\n  magic_enum::enum_contains(Color::GREEN); // -\u003e true\n  magic_enum::enum_contains\u003cColor\u003e(2); // -\u003e true\n  magic_enum::enum_contains\u003cColor\u003e(123); // -\u003e false\n  magic_enum::enum_contains\u003cColor\u003e(\"GREEN\"); // -\u003e true\n  magic_enum::enum_contains\u003cColor\u003e(\"fda\"); // -\u003e false\n  ```\n\n* Enum index in sequence\n\n  ```cpp\n  constexpr auto color_index = magic_enum::enum_index(Color::BLUE);\n  // color_index.value() -\u003e 1\n  // color_index.has_value() -\u003e true\n  ```\n\n* Functions for flags\n\n  ```cpp\n  enum Directions : std::uint64_t {\n    Left = 1,\n    Down = 2,\n    Up = 4,\n    Right = 8,\n  };\n  template \u003c\u003e\n  struct magic_enum::customize::enum_range\u003cDirections\u003e {\n    static constexpr bool is_flags = true;\n  };\n\n  magic_enum::enum_flags_name(Directions::Up | Directions::Right); // -\u003e \"Directions::Up|Directions::Right\"\n  magic_enum::enum_flags_contains(Directions::Up | Directions::Right); // -\u003e true\n  magic_enum::enum_flags_cast(3); // -\u003e \"Directions::Left|Directions::Down\"\n  ```\n\n* Enum type name\n\n  ```cpp\n  Color color = Color::RED;\n  auto type_name = magic_enum::enum_type_name\u003cdecltype(color)\u003e();\n  // type_name -\u003e \"Color\"\n  ```\n\n* IOstream operator for enum\n\n  ```cpp\n  using magic_enum::iostream_operators::operator\u003c\u003c; // out-of-the-box ostream operators for enums.\n  Color color = Color::BLUE;\n  std::cout \u003c\u003c color \u003c\u003c std::endl; // \"BLUE\"\n  ```\n\n  ```cpp\n  using magic_enum::iostream_operators::operator\u003e\u003e; // out-of-the-box istream operators for enums.\n  Color color;\n  std::cin \u003e\u003e color;\n  ```\n\n* Bitwise operator for enum\n\n  ```cpp\n  enum class Flags { A = 1 \u003c\u003c 0, B = 1 \u003c\u003c 1, C = 1 \u003c\u003c 2, D = 1 \u003c\u003c 3 };\n  using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for enums.\n  // Support operators: ~, |, \u0026, ^, |=, \u0026=, ^=.\n  Flags flags = Flags::A | Flags::B \u0026 ~Flags::C;\n  ```\n\n* Checks whether type is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration).\n\n  ```cpp\n  enum color { red, green, blue };\n  enum class direction { left, right };\n\n  magic_enum::is_unscoped_enum\u003ccolor\u003e::value -\u003e true\n  magic_enum::is_unscoped_enum\u003cdirection\u003e::value -\u003e false\n  magic_enum::is_unscoped_enum\u003cint\u003e::value -\u003e false\n\n  // Helper variable template.\n  magic_enum::is_unscoped_enum_v\u003ccolor\u003e -\u003e true\n  ```\n\n* Checks whether type is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations).\n\n  ```cpp\n  enum color { red, green, blue };\n  enum class direction { left, right };\n\n  magic_enum::is_scoped_enum\u003ccolor\u003e::value -\u003e false\n  magic_enum::is_scoped_enum\u003cdirection\u003e::value -\u003e true\n  magic_enum::is_scoped_enum\u003cint\u003e::value -\u003e false\n\n  // Helper variable template.\n  magic_enum::is_scoped_enum_v\u003cdirection\u003e -\u003e true\n  ```\n\n* Static storage enum variable to string\n  This version is much lighter on the compile times and is not restricted to the enum_range [limitation](doc/limitations.md).\n\n  ```cpp\n  constexpr Color color = Color::BLUE;\n  constexpr auto color_name = magic_enum::enum_name\u003ccolor\u003e();\n  // color_name -\u003e \"BLUE\"\n  ```\n\n* `containers::array` array container for enums.\n\n  ```cpp\n  magic_enum::containers::array\u003cColor, RGB\u003e color_rgb_array {};\n  color_rgb_array[Color::RED] = {255, 0, 0};\n  color_rgb_array[Color::GREEN] = {0, 255, 0};\n  color_rgb_array[Color::BLUE] = {0, 0, 255};\n  magic_enum::containers::get\u003cColor::BLUE\u003e(color_rgb_array) // -\u003e RGB{0, 0, 255}\n  ```\n\n* `containers::bitset` bitset container for enums.\n\n  ```cpp\n  constexpr magic_enum::containers::bitset\u003cColor\u003e color_bitset_red_green {Color::RED|Color::GREEN};\n  bool all = color_bitset_red_green.all();\n  // all -\u003e false\n  // Color::BLUE is missing\n  bool test = color_bitset_red_green.test(Color::RED);\n  // test -\u003e true\n  ```\n\n* `containers::set` set container for enums.\n\n  ```cpp\n  auto color_set = magic_enum::containers::set\u003cColor\u003e();\n  bool empty = color_set.empty();\n  // empty -\u003e true\n  color_set.insert(Color::GREEN);\n  color_set.insert(Color::BLUE);\n  color_set.insert(Color::RED);\n  std::size_t size = color_set.size();\n  // size -\u003e 3\n  ```\n\n* Improved UB-free \"SFINAE-friendly\" [underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type).\n\n  ```cpp\n  magic_enum::underlying_type\u003ccolor\u003e::type -\u003e int\n\n  // Helper types.\n  magic_enum::underlying_type_t\u003cDirection\u003e -\u003e int\n  ```\n## Remarks\n\n* `magic_enum` does not pretend to be a silver bullet for reflection for enums, it was originally designed for small enum.\n\n* Before use, read the [limitations](doc/limitations.md) of functionality.\n\n## Integration\n\n* You should add the required file [magic_enum.hpp](include/magic_enum/magic_enum.hpp), and optionally other headers from [include dir](include/) or [release archive](https://github.com/Neargye/magic_enum/releases/latest). Alternatively, you can build the library with CMake.\n\n* If you are using [vcpkg](https://github.com/Microsoft/vcpkg/) on your project for external dependencies, then you can use the [magic-enum package](https://github.com/microsoft/vcpkg/tree/master/ports/magic-enum).\n\n* If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add `magic_enum/x.y.z` to your conan's requires, where `x.y.z` is the release version you want to use.\n\n* If you are using [Build2](https://build2.org/) to build and manage your dependencies, add `depends: magic_enum ^x.y.z` to the manifest file where `x.y.z` is the release version you want to use. You can then import the target using `magic_enum%lib{magic_enum}`.\n\n* Alternatively, you can use something like [CPM](https://github.com/TheLartians/CPM) which is based on CMake's `Fetch_Content` module.\n\n  ```cmake\n  CPMAddPackage(\n      NAME magic_enum\n      GITHUB_REPOSITORY Neargye/magic_enum\n      GIT_TAG x.y.z # Where `x.y.z` is the release version you want to use.\n  )\n  ```\n\n* Bazel is also supported, simply add to your WORKSPACE file:\n\n  ```\n  http_archive(\n      name = \"magic_enum\",\n      strip_prefix = \"magic_enum-\u003ccommit\u003e\",\n      urls = [\"https://github.com/Neargye/magic_enum/archive/\u003ccommit\u003e.zip\"],\n  )\n  ```\n\n  To use bazel inside the repository it's possible to do:\n\n  ```\n  bazel build //...\n  bazel test //...\n  bazel run //example\n  ```\n\n  (Note that you must use a supported compiler or specify it with `export CC= \u003ccompiler\u003e`.)\n\n* If you are using [Ros](https://www.ros.org/), you can include this package by adding `\u003cdepend\u003emagic_enum\u003c/depend\u003e` to your package.xml and include this package in your workspace. In your CMakeLists.txt add the following:\n  ```cmake\n  find_package(magic_enum CONFIG REQUIRED)\n  ...\n  target_link_libraries(your_executable magic_enum::magic_enum)\n  ```\n\n## Compiler compatibility\n\n* Clang/LLVM \u003e= 5\n* MSVC++ \u003e= 14.11 / Visual Studio \u003e= 2017\n* Xcode \u003e= 10\n* GCC \u003e= 9\n* MinGW \u003e= 9\n\n## Licensed under the [MIT License](LICENSE)\n","funding_links":["paypal.me/Neargye","btc.com/btc/address/bc1qzevldln8tqz5xf4lyufu9msgl7t97xstth9zq8","etherscan.io/address/0xbb42fdef9204fa6f3d535d202b088dee35fcbd31","https://liberapay.com/neargye"],"categories":["Reflection","Libraries","C++","Coding","映射","C/C++程序设计"],"sub_categories":["C++","c++","物理学","Misc","资源传输下载"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNeargye%2Fmagic_enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNeargye%2Fmagic_enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNeargye%2Fmagic_enum/lists"}