I would like to have a struct (or something similar) in C++, that will allow access to its members dynamically. It should have a generic getter and setters that receive the member name as a string, and return some sort of variant type (e.g. boost::variant).
I was thinking it could be implemented using boost::fusion::map, by adding a string representing the name of each member, and building an STL map between strings and getter or setter functions. I don’t want to reinvent the wheel, so I was hoping something similar already existed.
What do you think? Would my idea work? Do you know other ways to accomplish my goal?
fusion is an approach, but why not store your “fields” in a
std::mapkeyed by astd::string, where the payload is theboost::variant…i.e.
and then you can just lookup the key in your getter/setter…
heck, wrap the
variantin anoptionaland you could have optional fields!a more complex example: