magic_enum for C++: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course
\n

magic_enum for C++: A Complete Guide to Enum Reflection

\n\n
\n

1. What is it and why is it needed?

\n

In the C++ language, enumerations (enum and enum class) are a powerful tool for creating named constants, but they have one significant drawback — the lack of built-in reflection. Standard C++ does not provide a simple way to get the string representation of an enum value, iterate over all its elements, or find out the number of variants. Developers have to manually write helper functions, macros, or use third-party code generators, which leads to code duplication and errors.

\n

The magic_enum library solves this problem by providing a powerful and convenient set of tools for working with enumerations at compile time and runtime. It uses the magic of C++17 templates and internal compiler mechanisms (e.g., __PRETTY_FUNCTION__) to automatically extract information about enumerations. With magic_enum, you can get the string name of any value, convert a string to a value, iterate over all enum elements, find out their count, and much more — without writing any boilerplate code.

\n

This library is indispensable for logging, serialization/deserialization, creating configuration files, parsing user input, testing, and any other tasks that require dynamic work with enumerations. It makes code cleaner, safer, and significantly reduces the amount of boilerplate code. magic_enum is arguably the most popular and mature library for enum reflection in C++, supported on all major compilers (GCC, Clang, MSVC).

\n
\n\n
\n

2. Installation

\n

magic_enum is a header-only library consisting of a single header file. This makes its installation incredibly simple. You don't need to build it as a separate library; just copy the magic_enum.hpp file into your project and include it.

\n

However, for convenient dependency management, it is recommended to use build systems like CMake (via FetchContent or find_package) or package managers such as vcpkg or Conan.

\n \n

Method 1: Download the file manually (simplest)

\n
    \n
  1. Go to the GitHub releases page: https://github.com/Neargye/magic_enum/releases
  2. \n
  3. Download the latest version of the magic_enum.hpp file.
  4. \n
  5. Place it in your project folder (e.g., in include/).
  6. \n
  7. Include it in your code: #include "magic_enum.hpp"
  8. \n
\n\n

Method 2: Via vcpkg (recommended for Windows)

\n
vcpkg install magic-enum
\n\n

Method 3: Via Conan

\n
conan install magic_enum/0.9.5@
\n\n

Method 4: Via CMake FetchContent (recommended for new projects)

\n

Add the following code to your CMakeLists.txt:

\n
include(FetchContent)\nFetchContent_Declare(\n  magic_enum\n  GIT_REPOSITORY https://github.com/Neargye/magic_enum.git\n  GIT_TAG v0.9.5 # Or the latest stable tag\n)\nFetchContent_MakeAvailable(magic_enum)\n\ntarget_link_libraries(your_target PRIVATE magic_enum::magic_enum)
\n

Important: magic_enum requires a compiler with C++17 support. Make sure you have enabled the appropriate standard in your project (e.g., set(CMAKE_CXX_STANDARD 17) or the -std=c++17 flag).

\n
\n\n
\n

3. Quick Start — Minimal Working Example

Recommendations