从std :: tuple <some_types ...> </some_types ...>开始创建子元组

时间:2013-07-25 09:34:42

标签: c++ templates c++11 variadic-templates stdtuple

让我们假设给出std::tuple<some_types...>。我想创建一个新的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 个答案:

答案 0 :(得分:5)

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

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>());
}

Live at Coliru

答案 1 :(得分:5)

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

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

另一种完全的方法是编写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的任意参数包中。但这不是您的问题所必需的。

答案 2 :(得分: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
}

Live example

答案 3 :(得分:0)

通过边界检查从元组中划分子范围,而无需声明“帮助程序类”:

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>