c++ - 在层次结构中反转可变参数模板参数
问题描述
我目前有这个使用类型列表的类:
template<class AbstractF, template<class, class > class Creator, class TList>
class A: public B<typename Reverse<TList>::Result, Creator, AbstractF> {
};
我试图使代码对 c++11 更友好,用可变参数模板参数替换类型列表。
template<class AbstractF, template<class, class > class Creator, class TList...>
class A: public B<Creator, AbstractF, TList...> { //<---how to reverse here?
};
问题是:如何反转可变参数列表?查看其他问题,我看到了 的用法std::tuple
,但在这种情况下,我无法理解如何“解包”它,因为可变参数不是简单模板函数的一部分,而是用作类层次结构中的参数。
解决方案
如果包中的所有类型都是不同的,则可以index
从类型本身计算类型。剩下的就是N - 1 - index
从包中提取 'th 类型:
template<std::size_t index, class... Ts>
using Nth_type = std::tuple_element_t<index, std::tuple<Ts...>>;
template<class S, class... Ts>
inline constexpr std::size_t type_index = 0;
template<class S, class T, class... Ts>
inline constexpr std::size_t type_index<S, T, Ts...> =
std::is_same_v<S, T> ? 0 : 1 + type_index<S, Ts...>;
template<class T, class... Ts>
using Reverser = Nth_type<sizeof...(Ts) - 1 - type_index<T, Ts...>, Ts...>;
简单的例子:
template<class... Ts>
class B;
template<class... Ts>
using RB = B<Reverser<Ts, Ts...>...>; // two pack expansions here
static_assert(std::is_same<RB<>, B<>>());
static_assert(std::is_same<RB<int>, B<int>>());
static_assert(std::is_same<RB<int, float, double>, B<double, float, int>>());
如果允许包包含相同的类型,则此技巧将不起作用。
此代码使用了一些 C++14/17 功能。变量模板不是 C++11 的一部分,因此type_index
必须以不同的方式实现。这是一个可能的实现。
推荐阅读
- blockchain - 基本 Quorum 网络设计
- javascript - 想要在按钮单击时动态更改 angular slickgrid 中的 gridoption preselectedRows 行(功能检查);
- powershell - 为开发人员自动更改配置文件
- json - 在特定位置添加 JSON 对象
- html - 在 HTML 和 CSS 中制作列表元素 (ul/li) 移动友好/响应式
- angular - 赛普拉斯测试失败,因为 Chrome 渲染器在 CI 中崩溃(使用无人机)
- javascript - iframe中按钮上的onclick功能
- unix - unix awk 命令根据匹配列合并两个表
- sql-server - 插入带有特殊字符的字符串时出现问题
- java - 如何在java中使用递归反转数字序列