views:

66

answers:

1

Is there an existing function (in boost mpl or fusion) to splat meta-vector to variadic template arguments? For example:

splat<vector<T1, T2, ...>, function>::type
// that would be the same as
function<T1, T2, ...>

My search have not found one, and I do not want to reinvent one if it already exists.

Alternatively, is there a solution for:

apply(f, t);
// that would be the same as
f(t[0], t[1], ...);

Given f is some template function and t is a fusion sequence.

edit: after some searching I found it in http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/functional/invocation/functions.html

+1  A: 

You need unpack_args and quoteN, where N is the number of template arguments your template takes. Alternatively if you implement function as a metafunction class, you don't need to quoteN. Example of a metafunction class yielding the first of two given types:

struct function1st {
  template<typename T1, typename T2>
  struct apply { typedef T1 type; };
};

/* create a metafunction class that takes a sequence and apply it to function1st */
typedef unpack_args<function1st> unpacker;

Then you can use unpacker as a metafunction class that takes a sequence

BOOST_MPL_ASSERT(( is_same< apply<unpacker, vector<int, char> >::type, int> ));

Or if you have it as a template, you need to quote it first

template<typename T1, typename T2>
struct function1st { typedef T1 type; };

/* create a metafunction class that takes a sequence and apply it to function1st */
typedef unpack_args< quote2<function1st> > unpacker;

Hope it helps.

Johannes Schaub - litb
thanks. unfortunately both approaches are somewhat difficult for me to implement because number of parameters is variable and I do not control function implementation. But there are other ways for me to solve it. but I will keep unpack and quote in mind, they are going to be quite useful for me
aaa
I found this, looking at structure of mpl invocation, which fixes my problem: http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/functional/invocation/functions.html
aaa