Skip to content

The Names Enums Throw Away

Edit source

Getting an enum's name back at compile time, which the language keeps refusing to give you.

Part 29 of 38ofFamiliars
  1. 1Session Zero
  2. 2Rolling for a Name
  3. 3The Haunted Handler
  4. 4The Sparse Wheel
  5. 5The Fork in the Path
  6. 6The Planet as a Prefix
  7. 7The Long Division
  8. 8The Line You Usually Cancel
  9. 9Sixteen Bytes Too Far
  10. 10Packing a UUID
  11. 11Bytes You Can Type
  12. 12The Shunting Yard
  13. 13Interchangeable Parts
  14. 14One Loop Per Core
  15. 15The Seam
  16. 16Append Only
  17. 17Who Owns the Tree
  18. 18Down the Flume
  19. 19The Syscalls That Lie
  20. 20Asking the Machine
  21. 21The Earth Is Not a Ball
  22. 22Smith and Schmidt
  23. 23Many Ways to Be Similar
  24. 24Built to Disappear
  25. 25Sometimes a Mutex Is Right
  26. 26Work, Not Threads
  27. 27Before the Program Runs
  28. 28No Collisions, Ever
  29. 29The Names Enums Throw Away
  30. 30The Bag of Holding
  31. 31A Map of Blocks
  32. 32Certainly Absent
  33. 33A Tree Made of Types
  34. 34Everything but the Views
  35. 35Who Owns the Wheel
  36. 36A Guest on the Loop
  37. 37Acknowledged Means Durable
  38. 38Sorted as Bytes

Part of Familiars: the small, sharp libraries I carved out of Xapiand. This one gives back what the language discards: enum-reflection.

Here is a small, permanent annoyance in C++. You write enum class Method { GET, POST, SEARCH, COMMIT };, and the compiler reads those names, understands them, and then throws them away. At runtime there is no way to ask “what is Method::SEARCH called,” because the string "SEARCH" does not exist in the program anymore; only the integer does. So every codebase does the same tedious thing, by hand: a switch that maps each member back to a string literal of its own name, for logging, and another mapping the other way, for parsing, both of which you must remember to update every time you add a member, and which drift out of sync the first time you forget. The names were right there at compile time. The language just does not keep them.

enum-reflection keeps them. Its ENUM_CLASS macro defines a strongly-typed enum class and, in the same breath, generates a set of constexpr accessors that reflect it: turn a member into its source-code name, look a name back up to its member. Everything is constexpr, so the enum-to-string mapping is not a runtime table you build at startup; it is compile-time data you can use in a static_assert.

The interesting part is how the name-to-member direction works, because that is a lookup from a string to an enum value, exactly the kind of dispatch the last two posts were building toward. The generated accessors route it through a compile-time perfect hash over the member names, built with the hashes macros. So looking a name up is not a chain of if (s == "GET") comparisons; it is a hash and a couple of array reads into a collision-free table, materialized at build time. The reflection is branch-light, heap-free, and constructs no runtime tables at all. This is the payoff of the whole compile-time chapter arriving in one place: a string switch wants a hash, the hash wants to be collision-free, and enum reflection is what you build once you have both, the enum names dispatched through a perfect hash the compiler computed for you.

It is also why this library sits at the top of a small stack of the others: it pulls in constexpr-phf for the dispatch and hashes for the hashing macros, and those pull their own siblings in turn. A reflective enum is not one idea; it is three of the smaller familiars standing on each other’s shoulders.

Alongside it lives utype, which is almost too small to mention and too useful to leave out. toUType(e) returns an enum’s value as its underlying integer type, the missing inverse of static_cast<EnumClass>(int):

enum class State : std::uint8_t { Idle = 0, Running = 2 };
auto n = toUType(State::Running); // std::uint8_t, value 2, exactly

It is constexpr and noexcept, and its whole point is that the result type is exactly std::underlying_type_t<E>, so it never silently widens the way a bare static_cast<int> would. It is one line of code, it has no dependencies, and it removes a paper-cut you feel every time you need an enum’s raw value without lying about its width. Some familiars are grand; some are just the right small thing.

Reach for enum-reflection whenever you serialize, log, parse, or debug enums and are tired of hand-maintaining two switch statements that drift. Reach for utype any time you want an enum’s underlying value with its exact type. Skip the reflection if your language or framework already has enum reflection (some do now), or if the enum is tiny and private and a hand-written pair of functions is genuinely less machinery than a macro.

That closes the compile-time chapter, and very nearly the campaign. One familiar remains, and it is not one familiar. It is the whole bag of small, honest tools too little for a post of their own, tipped out onto the table at once, and a look back at the party we have assembled.