C ++ meta-splat函数

时间:2010-05-29 17:31:51

标签: c++ templates boost metaprogramming

是否存在一个现有函数(在boost mpl或fusion中)将元向量转换为可变参数模板参数?例如:

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

我的搜索没有找到,如果已经存在,我不想重新发明一个。

或者,是否有解决方案:

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

鉴于 f 是一些模板函数而 t 是一个融合序列。

编辑:经过一些搜索后,我在http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/functional/invocation/functions.html

中找到了它

1 个答案:

答案 0 :(得分:1)

您需要unpack_argsquoteN,其中N是您的模板所采用的模板参数数量。或者,如果您将function实现为元函数类,则不需要quoteN。元函数类的示例,它产生两种给定类型中的第一种:

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;

然后你可以使用unpacker作为一个带序列的元函数类

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

或者如果您将其作为模板,则需要先引用它

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;

希望它有所帮助。