Skip to content
 

Converting Variadic Template Arguments Pack to Boost Mpl Sequence

Most existing articles describing variadic templates today address just some basic things, giving few encouraging examples, starting with recursive implementation of count<T...> method and usually finishing with type–safe printf, which by the way is very nice! :)

I’d like to add to these starter–oriented hints yet another one. The question risen usually when one first meets variadic templates is “a… is it possible to find out which types exactly were passed, like enumerate them and all sorts of such things?…” Usual answer refers to mentioned above count<T...> implementation only, saying that there could be little done with regards to the rest of problems. While it is true to some extent it is not actually.

This is indeed true if to be armed with pure compiler support only and have no time to create the whole required infrastructure. But wait, there already exist a number of excellent libraries dealing with C++ type sequences in all the ways allowing to get every bit of information from them. One of these is Boost MPL.

I won’t get into details with it: go on your own. Instead I’ll present a simple way to turn an arbitrary argument pack from variadic template to such a sequence. That would automatically mean you can have any information on the passed types you just might think of. Yes, you can count types, iterate through them, compare them do anything. The example I’ll give will be based on mpl::vector sequence. It doesn’t really matter which one to use, but vector is indexable, bidirectionally accessible so it might feet your needs the best.

The approach uses the same know recursive technique as is usually used in examples how to count the number of variadic template arguments.

//General definition of the helper class
template <typename ...Args> struct FromVariadic;

//This specialization does the actual job: it splits the whole pack
//into 2 parts: one single type T and the rest of types Args...
//As soon as it is done T is added to an mpl::vector.
//"bottom--up" recursion is used to fetch all types
template <class T, typename ...Args>
struct FromVariadic<T, Args...>
{
    typedef typename mpl::push_front<typename FromVariadic<Args...>::type, T>::type type;
};

//This is a specialization for the case when only one type is passed
//and also finishes recursive descent
template <class T>
struct FromVariadic<T>
{
    typedef mpl::vector<T> type;
};

//This one handles the case when no types where passed at all
template <>
struct FromVariadic<>
{
    typedef mpl::vector<> type;
};

Having this defined you can use it in the following way:

template <typename ...Args>
class A
{
    typedef typename FromVariadic<Args...>::type MplVector;

    //Do whatever you might imagine with MplVector ---
    //this is really a boost::mpl::vector !
};

And yes, this is compilable by gcc-4.4.1, with c++0x enabled.

One Comment

  1. Bx12 says:

    Congratulations on this post. I followed up on it on my website.

Leave a Reply