Part of Kronuz C++ Libraries, a collection of small, reusable C++ libraries. Read its story in The Names Enums Throw Away, a Familiars post.
Header-only reflective enums for C++20: one macro defines an enum class and constexpr perfect-hash lookups both ways.
ENUM_CLASS declares a real enum class and, from the same member list, lookups
on a compile-time perfect hash of each value and of each name, so enum_name and
enum_type are constexpr and build no table at run time.
Reach for it when enums appear in text: configuration, protocols, logs and
command lines.
- A real
enum class. With its underlying type and explicit values, dense or sparse. - Both directions.
enum_name(Status::NotFound)is"NotFound";enum_type<Status>("Teapot")isStatus::Teapot. constexprand allocation-free. Lookups work instatic_assert.- Perfect-hash dispatch. Through constexpr-phf and hashes; a hash hit is confirmed against the member's name with a few wide loads, so a string that collides with a member's hash is rejected.
- Misses your way.
enum_typethrowsstd::out_of_range;enum_try_typereturnsstd::nulloptfrom the same lookup. - Case-insensitive names. A name matches a member when the two are equal ignoring ASCII case.
#include "enum.h"
#include <cstdint>
#include <cstdio>
ENUM_CLASS(Status, std::uint16_t,
Ok = 200,
NotFound = 404,
Teapot = 418
)
static_assert(enum_name(Status::NotFound) == "NotFound"); // value to name, at compile time
static_assert(enum_type<Status>("Teapot") == Status::Teapot); // name to value
int main() {
auto s = enum_try_type<Status>("notfound"); // names match ignoring ASCII case
std::printf("%d\n", s ? static_cast<int>(*s) : -1); // 404
std::printf("%d\n", enum_try_type<Status>("Gone").has_value()); // 0: a miss, no exception
std::printf("[%.*s]\n", int(enum_name(static_cast<Status>(500)).size()),
enum_name(static_cast<Status>(500)).data()); // []: no member has the value 500
}examples/demo.cc walks every member of an enum without
naming one.
Requires a C++20 compiler and CMake 3.14 or newer; CMake fetches constexpr-phf and hashes, which bring static-string and char-classify. CI builds it on Linux (x86_64, arm64), macOS (arm64, x86_64), and FreeBSD, plus ASan, UBSan, and TSan builds.
include(FetchContent)
FetchContent_Declare(enum_reflection
GIT_REPOSITORY https://github.com/Kronuz/enum-reflection.git
GIT_TAG 90e837e9c6cac407aea406879cfa7ce0d2e33905) # or main for the tip
FetchContent_MakeAvailable(enum_reflection)
target_link_libraries(my_app PRIVATE enum_reflection::enum_reflection)#include "enum.h"Use it for enums you print, parse or read from configuration, instead of a name table kept by hand beside the enum.
Not for:
- Enums declared elsewhere. The macro declares the enum; it cannot reflect an existing one.
- Very large enums. Each name costs about 25 ms of compile time in every translation unit that uses the accessors, and the macros stop at 999 members.
- Expected misses through
enum_type. A miss that throws costs several hundred times a hit; callenum_try_typefor user input. - One answer for every miss. An unknown value gives
enum_namean empty string, while an unknown name makesenum_typethrow.
enum_reflection_bench times lookups cycling through the names in a shuffled order: the median of
nine runs of about a million lookups, in ns per lookup, Apple clang 17 with libc++ and -O3, on an
Apple M4 Pro (macOS 26, arm64) and an Intel Core i9-9980HK (macOS 15, x86-64). Each row is one
lookup on one enum, and each machine has two columns: enum-reflection is the library's call
and comparator is the alternative named in the row: enum_type hit finds a name that is a
member, enum_try_type miss one that is not, and enum_name finds the name of a value. The
comparators are absl::flat_hash_map
20260817.0 from std::string_view to value (Homebrew); magic_enum
v0.9.8, a widely used reflection library (enum_cast, enum_name; its range is widened to 256 for
the sparse enum, and enum_cast is case-sensitive where the library's lookup ignores case); a linear
scan of the names in order; and a binary search by value over the sorted (value, name) pairs.
Before timing, the benchmark checks that every comparator answers as enum-reflection does: each
member's name and value round-trip, and about eight near misses per member (one letter changed) are
rejected. Lower is faster.
| lookup | comparator | M4 Pro: enum-reflection | M4 Pro: comparator | i9-9980HK: enum-reflection | i9-9980HK: comparator |
|---|---|---|---|---|---|
enum_type hit, 32 members |
absl::flat_hash_map |
3.4 | 4.8 | 7.6 | 11.2 |
enum_type hit, 32 members |
linear scan | 3.4 | 17.2 | 7.6 | 43.1 |
enum_type hit, 32 members |
magic_enum::enum_cast |
3.4 | 17.6 | 7.6 | 44.4 |
enum_type hit, 128 members |
absl::flat_hash_map |
3.3 | 4.3 | 7.3 | 12.3 |
enum_type hit, 128 members |
magic_enum::enum_cast |
3.3 | 62.9 | 7.3 | 136.7 |
enum_try_type miss, 128 members |
absl::flat_hash_map |
2.8 | 2.1 | 6.3 | 5.0 |
enum_try_type miss, 128 members |
magic_enum::enum_cast |
2.8 | 39.3 | 6.3 | 70.7 |
enum_name, 32 sparse members |
binary search by value | 1.4 | 3.5 | 3.1 | 5.0 |
enum_name, 32 sparse members |
magic_enum::enum_name |
1.4 | 1.2 | 3.1 | 2.7 |
enum_name, 128 dense members |
magic_enum::enum_name |
1.6 | 0.4 | 3.8 | 1.0 |
cmake -B build -DCMAKE_BUILD_TYPE=Release -DENUM_REFLECTION_BUILD_BENCH=ON \
-DENUM_REFLECTION_BENCH_MAGIC_ENUM=ON
cmake --build build --target enum_reflection_bench
build/enum_reflection_benchA hit costs less than the hash map's at every size, and the map answers a miss 1.2x to 1.7x faster,
but it allocates and is built at run time. A linear scan and magic_enum::enum_cast take 5.1x to
5.8x as long as enum-reflection at 32 members, enum_cast 19x at 128, and both are about 3x faster
among 8 names. magic_enum::enum_name reads a table indexed by value: it is 1.2x faster at 32 sparse
members and 3.8x to 4x faster at 128 dense ones.
docs/perf/lookup.md has every size, enum_name against an array by value,
and the compile cost.
- The Names Enums Throw Away: the Familiars post on why it exists and how it works.
- docs/API.md:
ENUM_CLASS,ENUM, and the accessors. - ARCHITECTURE.md: the preprocessor map and the two perfect hashes.
- docs/perf/lookup.md: lookup and compile cost against the alternatives.
- CHANGELOG.md: what changed, with numbers.
- AGENTS.md: repository map, invariants, and traps.
- Kronuz C++ Libraries: the catalog entry.
cmake -B build
cmake --build build
ctest --test-dir build./build/enum_reflection_demo runs the demo, and enum_reflection_names tests
forged hash collisions. Add -DENUM_REFLECTION_BUILD_BENCH=ON to build
enum_reflection_bench; ctest then runs its --check mode, and with abseil
installed it adds the hash map rows.
Issues and pull requests are welcome. Read AGENTS.md and the catalog's rules of engagement first; CI must pass on every platform.
MIT, Copyright (c) 2026 Germán Méndez Bravo and Copyright (c) 2015-2020 Dubalu
LLC; enum.h carries portions Copyright (c) 2012-2019 Anton Bachin. See
LICENSE. Extracted from Xapiand.