Share your repls and programming experiences

← Back to all posts
Reflection in C++11
SPQR (533)

C++ does not natively support reflection (getting a reference to an object or function by its name in a string), so I made a header file which can (to some extent) emulate this functionality. This isn't super expandable but I'm sure some of you could find some use for it.

There are other, more useful ways of doing this, but those involve dynamic libraries and macros which, frankly, I don't understand, and regardless would be quite bothersome on replit without using polygott. (however if anyone wants me to I could take a stab at it)

Regardless, here is a little demonstration. Try typing "praise" or "suggest".


check out std::function, can make your code cleaner

SPQR (533)

@sanjaykdragon i actually use std::function in reflection.h, but I made the FuncBase and Func classes to allow for generic pointers in the std::map, otherwise i would have to specify return and parameter types for the std::functions in the std::map.


@SPQR didn't notice the header file, nice