omegaelit

A first look at static reflection

I have written the same struct-to-text code more times than I would like to admit. Reflection promises to make it disappear: describe the data once, and let the compiler walk the members.

With an experimental clang build the basic loop is short. You get the list of non-static data members of a type, splice each one back into an expression, and read its name as a string. That is enough for a printer, a command-line option parser and a simple binary format.

What still hurts: compile-time error messages are long, tooling support is uneven, and anything that needs attributes on members is clumsy until annotations land. But for config structs the payoff is immediate — one declaration, every consumer derived from it.