C ++ typelist сделать подсписок - PullRequest
4 голосов
/ 05 июня 2019

Допустим, у меня есть тип

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

Ответы [ 3 ]

5 голосов
/ 05 июня 2019

Как обычно, 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;

Демо

2 голосов
/ 05 июня 2019

Это может быть излишним, но это работает:

template<typename... Ts>
struct typelist {};

template<class Typelist, typename T>
struct prepend;

template<typename... Ts, typename T>
struct prepend<typelist<Ts...>, T> {
    using type = typelist<T, Ts...>;
};

template<int start, int stop, int i, typename... Ts>
struct sublist_impl {
    using type = typelist<>;
};

template<int start, int stop, int i, typename T, typename... Ts>
struct sublist_impl<start, stop, i, T, Ts...>
{
private:
    static constexpr auto get_sublist_type() {
        if constexpr (i < start)
            return typename sublist_impl<start, stop, i + 1, Ts...>::type{};
        else if constexpr (i < stop)        
            return typename prepend<typename sublist_impl<
                start, stop, i + 1, Ts...>::type, T>::type{};
        else
            return typelist<>{};
    }

public:
    using type = decltype(get_sublist_type());
};

template<int start, int stop, typename... Ts>
struct sublist {
    using type = typename sublist_impl<start, stop, 0, Ts...>::type;
};

template<int start, int stop, typename... Ts>
using sublist_t = typename sublist<start, stop, Ts...>::type;

static_assert(std::is_same_v<
    sublist_t<1, 3, int, float, double, char>, typelist<float, double>>);

static_assert(std::is_same_v<
    sublist_t<0, 0, int, float, double, char>, typelist<>>);

static_assert(std::is_same_v<
    sublist_t<4, 4, int, float, double, char>, typelist<>>);

static_assert(std::is_same_v<
    sublist_t<0, 3, int, float, double, char>, typelist<int, float, double>>);

static_assert(std::is_same_v<
    sublist_t<0, 4, int, float, double, char>, typelist<int, float, double, char>>);
1 голос
/ 05 июня 2019

Просто для удовольствия, std::tuple_cat() способ

#include <tuple>
#include <type_traits>

template<typename ...Ts>
struct typelist
 { };

template <std::size_t sI, std::size_t sE, std::size_t I, typename T>
constexpr std::enable_if_t<(I >= sI) && (I < sE),
                           std::tuple<typelist<T>>> getTpl ();

template <std::size_t sI, std::size_t sE, std::size_t I, typename T>
constexpr std::enable_if_t<(I < sI) || (I >= sE),
                           std::tuple<>> getTpl ();

template <typename ... Ts>
constexpr typelist<Ts...> getList (std::tuple<typelist<Ts>...>);

template <std::size_t sI, std::size_t sE, typename ... Ts,
          std::size_t ... Is>
constexpr auto getTplList (typelist<Ts...>, std::index_sequence<Is...>)
   -> decltype( getList(std::tuple_cat(getTpl<sI, sE, Is, Ts>()...)) );

template <std::size_t startI, std::size_t stopE, typename ... Ts>
struct sublist
 {
   using type = decltype(getTplList<startI, stopE>
                         (typelist<Ts...>{},
                          std::index_sequence_for<Ts...>{}));
 };

int main ()
 {
   using type1 = typename sublist<1u, 3u, int, float, double, char>::type;
   using type2 = typelist<float, double>;

   static_assert( std::is_same<type1, type2>::value, "!" );
 }
...