Adam Ryczkowski
Adam Ryczkowski

Reputation: 8064

c++: how to create a variadic sequence from struct members

Suppose I have a templated class that is supposed to be called like this:

struct mystruct
{
   int    member1;
   long   member2;
   string member3;
};

Now I want to create a boost::mpl::vector that will effectively contain types of all the members in the struct (preferably in the same order):

using membervector1 = boost::mpl::vector<int, long, string>;

Of course I want the syntax to templated with respect to my struct, like this:

using membervector2 = some_smart_template<mystruct>;
static_assert(std::is_same<membervector1, membervector2>::value);

Upvotes: 0

Views: 94

Answers (1)

Alex Guteniev
Alex Guteniev

Reputation: 13679

C++ static reflaction can be implemented in C++14 aready.

Magic get almost does what you want, except that it's tuple, not mpl::vector, but I think it is not a big issue to get mpl::vector from tuple.

See presentation slides with explaination.

Upvotes: 1

Related Questions