I'm doing something similar in C++ for vertex attributes, integration with Lua et.al. First I define the struct:
INTROSPECTION_DEFINE_STRUCT(S,
(int, anInt),
(bool, aBool),
(float, aFloat),
(double, aDouble),
(std::string, aString)
);
Then I create an instance of it: S s{ 23, true, 37.5f, 42.25, "String" };
And finally I can e.g. iterate over all fields and output their value using a generic lambda: forEachField(s, [](auto v) {
std::cout << v;
});
There are also definitions for the field's type, offset, name etc. generated by the macro, so it's easy to create a binding for vertex attributes, map the struct to a table in Lua, serialize/deserialize it and so on.I'm really hoping C++ gets introspection soon. There are some proposals like https://isocpp.org/files/papers/n3996.pdf and a working group at https://groups.google.com/a/isocpp.org/forum/#!forum/reflect... but I'm not holding my breath for 2017.