c++ - C++ typelist 制作子列表
问题描述
假设我有一个类型
template<typename ...Ts>
struct typelist {};
我需要从此列表中获取子列表:
template<int startInclusive, int stopExclusive, typename ...Ts>
struct sublist {
using type = ?; //
};
例如
sublist<1, 3, int, float, double, char>::type == typelist<float, double>
当start = 0
我有一个有效的尾部实现时:
template<typename ...Ts>
struct typelist {};
template<int N, typename T, typename ...Ts>
struct tail {
using type = typename tail<N - 1, Ts...>::type;
};
template<typename T, typename ...Ts>
struct tail<0, T, Ts...> {
using type = typelist<T, Ts...>;
};
using T = tail<1, int, double>::type;
#include <typeinfo>
#include <cstdio>
int main() {
::printf("%s\n", typeid(T).name());
}
但是,我无法得到任何工作start > 0
解决方案
像往常一样,std::index_sequence
这里有帮助:
template <std::size_t Offset, typename Seq, typename Tuple> struct sublist_impl;
template <std::size_t Offset, std::size_t ... Is, typename Tuple>
struct sublist_impl<Offset, std::index_sequence<Is...>, Tuple>
{
using type = std::tuple<std::tuple_element_t<Offset + Is, Tuple>...>;
};
template<std::size_t startInclusive, std::size_t stopExclusive, typename ...Ts>
using sublist = typename sublist_impl<startInclusive,
std::make_index_sequence<stopExclusive - startInclusive>,
std::tuple<Ts...>>::type;
推荐阅读
- c# - 如何在Sqlite c#中使用WHERE BETWEEN从数据库中检索数据
- c# - 方法找不到在同一类 C# 中实例化的公共列表
- python - 浅拷贝不应该在python中创建一个具有不同id的对象吗?
- mysql - 根据其他表列MYSQL的计算将唯一记录插入表中
- python - 替换数据框中存在的单词
- python - 当 PermissionDenied 异常引发时 Django 打印错误
- java - 日期函数为伊斯兰日期获得额外的一天 - Kotlin
- json - 具有最大值和最小值的 JSON Schema 枚举
- saml-2.0 - Blazor 服务器端 - SAML2 身份验证
- dolphindb - 计算累积的“出价”和“要价”比率