Background
Background

C++ STL Reflected Enums – The Cursed Edition

While building my own game engine to learn, I experimented with an enum-name table. An enum groups named constant values; the aim was to retrieve those names as text. My original constraints were C++11, the standard library and DirectX 11, with no other support libraries.

Historical C++ usage code declaring Dog, Bird and Mouse with REnum, then streaming the table entry indexed by EUnitTest::Dog to std::cout.
The original usage example: source code, with no captured output or build result.

The essential declaration and lookup from that example:

REnum(UnitTest, Dog, Bird, Mouse)
void UnitTestAEnum()
{
std::cout << "Entry: " << RUnitTest::Strings[EUnitTest::Dog] << std::endl;
}

REnum is a custom variadic macro: a preprocessor definition accepting a variable number of arguments. Here it declares the unscoped enum EUnitTest and companion struct RUnitTest. The lookup uses the enum’s numeric value as an array index, assuming consecutive values starting at zero. Support for assigned values, gaps, negative values or aliases—different names sharing a value—is not established.

Generating the name table

The macro turns its argument tokens into text for custom processing. It generates the enum and its name table together; it does not inspect arbitrary existing enums.

The intended pipeline uses GetTokenCount to determine the array size, then passes the text and ProcessEnumStrings to the factory function CreateCEXPArray. That factory constructs TCEXPArray, a fixed-size container of string views. Each view identifies a character range without owning its storage. These counting, processing and container helpers are all custom code.

The goal was to build the table during compilation using a constexpr initializer. static constexpr auto remains the most bewitched line I have written.

The comma-separated input meets helpers using space delimiters. Missing definitions leave token counts, comma removal, spacing changes and assignment expressions unverified. This is an experimental enum-name table, with no demonstrated general-purpose parser.

Macro expansion and the incomplete token helpers

## pastes tokens, forming E##name and R##name. #__VA_ARGS__ stringizes the argument tokens into a string literal. Stringization normalizes whitespace; it does not parse C++ enumerator declarations.

REnum macro defining an unscoped E-prefixed enum and an R-prefixed struct whose Strings member calls CreateCEXPArray with stringized arguments and ProcessEnumStrings.
The historical macro connects the enum declaration to the proposed name table.

GetTokenCount loops through positions returned by custom FindFirstNotOf and FindFirstOf helpers. Its default delimiter is a literal space. ProcessEnumStrings also passes a space to custom GetTokenFromIndex while filling the destination. Those search and extraction definitions are absent; there is no standard-library GetTokenCount behind them.

The surviving processor also declares template<int Size> and a function parameter named int Size. That redeclares a template parameter within its scope (N4659, §17.6.1/6). It is a defect in the fragment, not a working implementation to copy.

Factory signatures, fixed-size storage and array access

TCEXPArray<T, Size> stores a private T Data[Size]. Both constructors initialize that array; the second invokes a supplied processor with the destination, input and size. Its callback is restricted to the declared function-pointer signature.

TCEXPArray template with a private fixed-size Data array, two constexpr constructors, a processor callback taking T pointer, T and int, and two non-constexpr indexing operators.
The support structure separates array construction from access through its indexing operators.

The macro calls generic CreateCEXPArray, whose screenshot declares void(*)(T, T, int). The constructor above expects a callback of type void(*)(T*, T, int): the processor receives a pointer to the destination array’s first element. The shown signatures do not match.

A separate custom CreateEnumArray factory specializes the element type to std::basic_string_view<char>. Its callback’s array-parameter notation adjusts to a pointer under the function parameter rules (N4659, §11.3.5/5), so that spelling is not another mismatch. It is not the factory called by the macro.

The two operator[] overloads return array elements directly, without bounds checks or constexpr. Neither property is supplied merely by making the stored table constant.

Language version and prototype limits

The surviving sample goes beyond C++11: it uses std::basic_string_view<char> and [[nodiscard]], an attribute requesting warnings for discarded results. Both were standardized in C++17. The loops, local mutation and constexpr void also depend on C++14’s relaxed constant-expression rules. Selecting a newer language mode would not repair the separate declaration and callback defects.

A constexpr function can also execute at runtime. Constant evaluation constrains the operations and calls on the path actually evaluated, rather than every function mentioned in its body. Constant table construction would not make lookup through the shown non-constexpr indexing operators a constant expression, or establish a performance benefit.

A bit of chaos, but a fun learning experience.

Move over the bright dot and follow the line. P pauses. Escape closes the game.

Sigil Trace

Move over the sigil before time runs out.

Score0000
Spells00
Best on this browser0000
I · KINDLE SPELL 01
START

Trace the Sigil with your mouse

Start at the glowing dot and follow the line. No click needed.

or press Enter
Move your mouse onto the bright dot. No click needed.