No, in C ++ there is no way to find out the names of all the participants or how many members are actually there.
You can store all types in mpl::vector along with your classes, but then you are faced with the problem of how to turn them into members with the corresponding names (which you cannot achieve without any hacking).
Using std::tuple instead of POD is a solution that usually works, but it creates incredible messy code when you actually work with a tuple (without variable names) if you don't convert it at any moment or don't have a shell, which forwards accessors to a member of the tuple.
class message { public: // ctors const int& foo() const { return std::get<0>(data); } // continue boiler plate with const overloads etc static std::size_t nun_members() { return std::tuple_size<data>::value; } private: std::tuple<int, long long, foo> data; };
Solution with Boost.PP and MPL:
#include <boost/mpl/vector.hpp> #include <boost/mpl/at.hpp> #include <boost/preprocessor.hpp> #include <boost/preprocessor/arithmetic/inc.hpp> struct Foo { typedef boost::mpl::vector<int, double, long long> types; // corresponding type names here #define SEQ (foo)(bar)(baz) #define MACRO(r, data, i, elem) boost::mpl::at< types, boost::mpl::int_<i> >::type elem; BOOST_PP_SEQ_FOR_EACH_I(MACRO, 0, SEQ) }; int main() { Foo a; a.foo; }
I did not test it so that there might be errors.
source share