Anycorn
Anycorn

Reputation: 51445

C++ meta-splat function

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

Upvotes: 1

Views: 1857

Answers (1)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506857

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.

Upvotes: 1

Related Questions