c++ - 如何将 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() 调用 doSomething() 并将 mpl 中的类型设置为模板参数。就像是:
void doSomethingForAll()
{
//pseudocode:
doSomething<expandTypesToTemplateParameters<MyContainer::type>>();
}
这可能吗?
解决方案
template<class...> class TT
本质上,您需要一个将 a与 a映射mpl::set<Ts...>
到的提升函数TT<Ts...>
。
lifter
一般而言,要做到这一点,请在 Foldable 的帮助下编写一个:
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;
};
};
然后你可以这样使用lift 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;
然后u
is foo<int, long>
,v
isfoo<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_();
}
推荐阅读
- java - 最小回文数大于 N
- python - 从 cisco 设备连接到 debian 上的 http 服务器
- redis - redis 多数据库与单数据库,哪个最好?
- svg - flex div容器内的内联SVG(可能有百分比大小)
- r - 交叉引用和标题在 Rmd 文件中不起作用
- c++ - C++20 是否为“溢出”的有符号整数定义了左移?
- python - 如何在一段时间没有活动后禁用 selenium chrome 驱动程序的自动关闭操作?
- python - 在 pyCUDA 中使用 CUDA 类型
- pyspark - 如何在 pyspark 中获得确定性随机排序?
- postgresql - bucardo 同步问题,功能不存在