6

让我们假设std::tuple<some_types...>给定a。我想创建一个新std::tuple的,其类型是在[0, sizeof...(some_types) - 2]. 例如,假设起始元组是std::tuple<int, double, bool>. 我想获得一个定义为的子元组std::tuple<int, double>

我对可变参数模板很陌生。作为第一步,我尝试编写一个struct负责存储不同类型的原始std::tuple文件,目的是创建一个相同类型的新元组(如 中std::tuple<decltype(old_tuple)> new_tuple)。

template<typename... types>
struct type_list;

template<typename T, typename... types>
struct type_list<T, types...> : public type_list<types...>  {
    typedef T type;
};


template<typename T>
struct type_list<T> {
    typedef T type;
};

我想做的是:

std::tuple<type_list<bool, double, int>::type...> new_tuple // this won't work

下一步是丢弃参数包中的最后一个元素。我怎样才能访问type存储的几个type_list?以及如何丢弃其中一些?

谢谢。

4

4 回答 4

9

这是一种直接解决您的问题的方法。

template<unsigned...s> struct seq { typedef seq<s...> type; };
template<unsigned max, unsigned... s> struct make_seq:make_seq<max-1, max-1, s...> {};
template<unsigned...s> struct make_seq<0, s...>:seq<s...> {};

template<unsigned... s, typename Tuple>
auto extract_tuple( seq<s...>, Tuple& tup ) {
  return std::make_tuple( std::get<s>(tup)... );
}

您可以按如下方式使用它:

std::tuple< int, double, bool > my_tup;
auto short_tup = extract_tuple( make_seq<2>(), my_tup );
auto skip_2nd = extract_tuple( seq<0,2>(), my_tup );

decltype在您需要结果类型时使用。

另一种完全不同的方法是 write append_type,它接受一个类型和一个tuple<...>,并将该类型添加到末尾。然后添加到type_list

template<template<typename...>class target>
struct gather {
  typedef typename type_list<types...>::template gather<target>::type parent_result;
  typedef typename append< parent_result, T >::type type;
};

这为您提供了一种将您的类型累积type_list到任意参数包中的方法template。但这不是您的问题所必需的。

于 2013-07-25T11:13:15.903 回答
6

使用索引序列技术,这种操作相当容易:生成一个索引序列,其索引比您的元组少两个,然后使用该序列从原始序列中选择字段。从 C++14使用std::make_index_sequence和返回类型推导:

template <typename... T, std::size_t... I>
auto subtuple_(const std::tuple<T...>& t, std::index_sequence<I...>) {
  return std::make_tuple(std::get<I>(t)...);
}

template <int Trim, typename... T>
auto subtuple(const std::tuple<T...>& t) {
  return subtuple_(t, std::make_index_sequence<sizeof...(T) - Trim>());
}

在 C++11 中:

#include <cstddef>     // for std::size_t

template<typename T, T... I>
struct integer_sequence {
  using value_type = T;

  static constexpr std::size_t size() noexcept {
    return sizeof...(I);
  }
};

namespace integer_sequence_detail {
template <typename, typename> struct concat;

template <typename T, T... A, T... B>
struct concat<integer_sequence<T, A...>, integer_sequence<T, B...>> {
  typedef integer_sequence<T, A..., B...> type;
};

template <typename T, int First, int Count>
struct build_helper {
  using type = typename concat<
    typename build_helper<T, First,           Count/2>::type,
    typename build_helper<T, First + Count/2, Count - Count/2>::type
  >::type;
};

template <typename T, int First>
struct build_helper<T, First, 1> {
  using type = integer_sequence<T, T(First)>;
};

template <typename T, int First>
struct build_helper<T, First, 0> {
  using type = integer_sequence<T>;
};

template <typename T, T N>
using builder = typename build_helper<T, 0, N>::type;
} // namespace integer_sequence_detail

template <typename T, T N>
using make_integer_sequence = integer_sequence_detail::builder<T, N>;

template <std::size_t... I>
using index_sequence = integer_sequence<std::size_t, I...>;

template<size_t N>
using make_index_sequence = make_integer_sequence<size_t, N>;

#include <tuple>

template <typename... T, std::size_t... I>
auto subtuple_(const std::tuple<T...>& t, index_sequence<I...>) 
  -> decltype(std::make_tuple(std::get<I>(t)...))
{
  return std::make_tuple(std::get<I>(t)...);
}

template <int Trim, typename... T>
auto subtuple(const std::tuple<T...>& t)
  -> decltype(subtuple_(t, make_index_sequence<sizeof...(T) - Trim>()))
{
  return subtuple_(t, make_index_sequence<sizeof...(T) - Trim>());
}

住在科利鲁

于 2013-07-25T11:10:50.380 回答
1

一种方法是递归地将两个元组传递给一个辅助结构,该结构采用“源”元组的第一个元素并将其添加到另一个元组的末尾:

#include <iostream>
#include <tuple>
#include <type_traits>

namespace detail {

    template<typename...>
    struct truncate;

    // this specialization does the majority of the work

    template<typename... Head, typename T, typename... Tail>
    struct truncate< std::tuple<Head...>, std::tuple<T, Tail...> > {
        typedef typename
        truncate< std::tuple<Head..., T>, std::tuple<Tail...> >::type type;
    };

    // this one stops the recursion when there's only
    // one element left in the source tuple

    template<typename... Head, typename T>
    struct truncate< std::tuple<Head...>, std::tuple<T> > {
        typedef std::tuple<Head...> type;
    };
}

template<typename...>
struct tuple_truncate;

template<typename... Args>
struct tuple_truncate<std::tuple<Args...>> {

    // initiate the recursion - we start with an empty tuple,
    // with the source tuple on the right

    typedef typename detail::truncate< std::tuple<>, std::tuple<Args...> >::type type;
};

int main()
{
    typedef typename tuple_truncate< std::tuple<bool, double, int> >::type X;

    // test
    std::cout << std::is_same<X, std::tuple<bool, double>>::value; // 1, yay
}

活生生的例子

于 2013-07-25T09:54:24.377 回答
1

带有边界检查的元组的子范围,没有声明“帮助类”:

template <size_t starting, size_t elems, class tuple, class seq = decltype(std::make_index_sequence<elems>())>
struct sub_range;

template <size_t starting, size_t elems, class ... args, size_t ... indx>
struct sub_range<starting, elems, std::tuple<args...>, std::index_sequence<indx...>>
{
    static_assert(elems <= sizeof...(args) - starting, "sub range is out of bounds!");
    using tuple = std::tuple<std::tuple_element_t<indx + starting, std::tuple<args...>> ...>;
};

用法:

struct a0;
...
struct a8;

using range_outer = std::tuple<a0, a1, a2, a3, a4, a5, a6, a7, a8>;
sub_range<2, 3, range_outer>::tuple; //std::tuple<a2, a3, a4>
于 2019-08-17T14:46:23.413 回答