gpt4 book ai didi

C++ typelist make 子列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:38 27 4
gpt4 key购买 nike

假设我有一个类型

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 我有一个有效的 tail 实现:

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;

Demo

关于C++ typelist make 子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com