Scripts2026年7月24日·1 分钟阅读

magic_enum — Static Reflection for C++ Enums

magic_enum is a header-only C++17 library that provides static reflection for enums: converting enums to strings, strings to enums, and iterating over enum values without macros or boilerplate.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
magic_enum
直接安装命令
npx -y tokrepo@latest install 37aea2b3-87a2-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

magic_enum provides static reflection capabilities for C++ enumerations using compiler intrinsics. Without macros or code generation, it turns any enum into something you can convert to/from strings, iterate over, and inspect at compile time.

What magic_enum Does

  • Converts enum values to their string names and vice versa at compile time
  • Iterates over all values of an enum without maintaining a separate list
  • Provides compile-time enum count, min, max, and contains checks
  • Supports scoped enums, unscoped enums, and flag/bitmask enums
  • Works with enum values in custom ranges via template specialization

Architecture Overview

magic_enum leverages compiler-specific intrinsics (PRETTY_FUNCTION on GCC/Clang, FUNCSIG on MSVC) to extract enum value names at compile time. It generates a lookup table of valid enum values and their string representations during compilation, incurring zero runtime overhead for name lookups.

Self-Hosting & Configuration

  • Single header file: download magic_enum.hpp or install via vcpkg/Conan
  • Include the header and use directly; no linking or build configuration needed
  • Customize the default enum range with MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX
  • For flag enums, use magic_enum::flags functions instead of the default enum functions
  • Requires C++17; works with GCC 9+, Clang 5+, and MSVC 2019+

Key Features

  • Zero runtime overhead: all reflection data is computed at compile time
  • No macros required; works with existing enum definitions unmodified
  • Supports bidirectional enum-string conversion with optional case-insensitive matching
  • Flag enum support for bitmask types with automatic combination name generation
  • Compile-time enum validation and static_assert integration

Comparison with Similar Tools

  • Manual switch/case — Hand-written mappings are error-prone and must be updated with every enum change; magic_enum stays in sync automatically
  • X-macro pattern — X-macros work but require defining enums in a non-standard way; magic_enum works with standard enum declarations
  • Boost.Describe — Boost.Describe requires annotating types; magic_enum needs no annotations
  • nameof — nameof provides general name reflection; magic_enum is specialized for enums with richer features like iteration and casting

FAQ

Q: Does magic_enum work with all enum values? A: By default it covers enum values in the range [-128, 127]. Customize with MAGIC_ENUM_RANGE_MIN/MAX for larger ranges.

Q: Is there a runtime cost? A: No. String tables are built at compile time. Lookups are simple array accesses.

Q: Does it work with enum classes (scoped enums)? A: Yes. Both scoped (enum class) and unscoped (enum) types are fully supported.

Q: Can I use magic_enum with flags/bitmask enums? A: Yes. Use magic_enum::flags_name and related functions for bitmask enums that combine multiple values.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产