如何将mpl::set的内容扩展为函数模板的模板参数



我有一个函数模板,模板参数的数量各不相同:

template <typename T1, typename... Ts>
void doSomething()
{
...
}

此外,我有一个mpl集定义如下:

template <typename... Ts>
struct MyContainerCreator
{
using type = boost::mpl::set<Ts...>;
};
using MyContainer= MyContainerCreator<T1, T2>;

现在我想编写一个函数doSomethingForAll((,它调用doSometing((,并将mpl中的类型设置为模板参数。类似于:

void doSomethingForAll()
{
//pseudocode:
doSomething<expandTypesToTemplateParameters<MyContainer::type>>();
}

这可能吗?

本质上,您需要一个提升函数,将具有mpl::set<Ts...>template<class...> class TT映射到TT<Ts...>

一般来说,在Foldable:的帮助下编写一个lifter

template<template<class...> class TT>
struct lifter {
template<class Foldable>
struct apply {
template <class Left, class Current> struct one_stepper;
template<class... Ts, class Current>
struct one_stepper<TT<Ts...>, Current> {
using type = TT<Ts..., Current>;
};
using type = typename mpl::fold<Foldable, TT<>,
one_stepper<mpl::_1, mpl::_2>
>::type;
};
};

然后你可以用这种方式使用提升mpl集装箱:

template<class... Ts> struct foo {};
using u = lifter<foo>::apply<mpl::vector<int, long>>::type;
using v = lifter<foo>::apply<mpl::set<int, long>>::type;

ufoo<int, long>vfoo<int, long>foo<long, int>取决于mpl的实现。

使用此工具,您的任务可以通过以下方式完成:

template<class... Ts>
struct doSomething_helper {
static void do_() { doSomething<Ts...>(); }
};
void doSomethingForAll()
{
//pseudocode:
// doSomething<expandTypesToTemplateParameters<MyContainer::type>>();
lifter<doSomething_helper>::apply<typename MyContainer::type>::type::do_();
}