得到std :: tuple的一部分

Ria*_*iaD 24 c++ tuples g++ c++11

我有一个未知大小的元组(它是方法的模板参数)

是否可以获得它的一部分(我需要丢掉它的第一个元素)

例如,我有tuple<int,int,int>(7,12,42).我想要tuple<int,int>(12,42)在这里

ken*_*ytm 15

借助编译时整数列表:

#include <cstdlib>

template <size_t... n>
struct ct_integers_list {
    template <size_t m>
    struct push_back
    {
        typedef ct_integers_list<n..., m> type;
    };
};

template <size_t max>
struct ct_iota_1
{
    typedef typename ct_iota_1<max-1>::type::template push_back<max>::type type;
};

template <>
struct ct_iota_1<0>
{
    typedef ct_integers_list<> type;
};
Run Code Online (Sandbox Code Playgroud)

我们可以通过参数包扩展来构造尾部:

#include <tuple>

template <size_t... indices, typename Tuple>
auto tuple_subset(const Tuple& tpl, ct_integers_list<indices...>)
    -> decltype(std::make_tuple(std::get<indices>(tpl)...))
{
    return std::make_tuple(std::get<indices>(tpl)...);
    // this means:
    //   make_tuple(get<indices[0]>(tpl), get<indices[1]>(tpl), ...)
}

template <typename Head, typename... Tail>
std::tuple<Tail...> tuple_tail(const std::tuple<Head, Tail...>& tpl)
{
    return tuple_subset(tpl, typename ct_iota_1<sizeof...(Tail)>::type());
    // this means:
    //   tuple_subset<1, 2, 3, ..., sizeof...(Tail)-1>(tpl, ..)
}
Run Code Online (Sandbox Code Playgroud)

用法:

#include <cstdio>

int main()
{
    auto a = std::make_tuple(1, "hello", 7.9);
    auto b = tuple_tail(a);

    const char* s = nullptr;
    double d = 0.0;
    std::tie(s, d) = b;
    printf("%s %g\n", s, d);
    // prints:   hello 7.9

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

(关于ideone:http://ideone.com/Tzv7v ;代码适用于g ++ 4.5到4.7和clang ++ 3.0)


Ada*_*itz 9

可能有一种更简单的方法,但这是一个开始."tail"函数模板返回一个复制的元组,其中包含除第一个之外的所有原始值.这在C++ 0x模式下与GCC 4.6.2一起编译.

template<size_t I>
struct assign {
  template<class ResultTuple, class SrcTuple>
  static void x(ResultTuple& t, const SrcTuple& tup) {
    std::get<I - 1>(t) = std::get<I>(tup);
    assign<I - 1>::x(t, tup);
  }
};

template<>
struct assign<1> {
  template<class ResultTuple, class SrcTuple>
  static void x(ResultTuple& t, const SrcTuple& tup) {
    std::get<0>(t) = std::get<1>(tup);
  }
};


template<class Tup> struct tail_helper;

template<class Head, class... Tail>
struct tail_helper<std::tuple<Head, Tail...>> {
  typedef typename std::tuple<Tail...> type;
  static type tail(const std::tuple<Head, Tail...>& tup) {
    type t;
    assign<std::tuple_size<type>::value>::x(t, tup);
    return t;
  }
};

template<class Tup>
typename tail_helper<Tup>::type tail(const Tup& tup) {
  return tail_helper<Tup>::tail(tup);
}
Run Code Online (Sandbox Code Playgroud)


Jar*_*d42 8

使用C++ 17,您可以使用std::apply:

template <typename Head, typename... Tail>
std::tuple<Tail...> tuple_tail(const std::tuple<Head, Tail...>& t)
{
    return apply([](auto head, auto... tail) {
        return std::make_tuple(tail...)};
    }, t);
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*son 6

我对Adam的代码进行了一些修改,它将剥离元组的前N个参数,并创建一个只包含最后N个类型的新元组......这是完整的代码(注意:如果有人决定+1我的话回答,也请+1亚当的答案,因为这是这个代码的基础,我不希望从他的贡献中拿走任何信用):

//create a struct that allows us to create a new tupe-type with the first
//N types truncated from the front

template<size_t N, typename Tuple_Type>
struct tuple_trunc {};

template<size_t N, typename Head, typename... Tail>
struct tuple_trunc<N, std::tuple<Head, Tail...>>
{
    typedef typename tuple_trunc<N-1, std::tuple<Tail...>>::type type;
};

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

/*-------Begin Adam's Code-----------

Note the code has been slightly modified ... I didn't see the need for the extra
variadic templates in the "assign" structure.  Hopefully this doesn't break something
I didn't forsee

*/

template<size_t N, size_t I>
struct assign 
{
    template<class ResultTuple, class SrcTuple>
    static void x(ResultTuple& t, const SrcTuple& tup) 
    {
        std::get<I - N>(t) = std::get<I>(tup);  
        assign<N, I - 1>::x(t, tup);  //this offsets the assignment index by N
    }
};

template<size_t N>
struct assign<N, 1> 
{
    template<class ResultTuple, class SrcTuple>
    static void x(ResultTuple& t, const SrcTuple& tup) 
    {
        std::get<0>(t) = std::get<1>(tup);
    }
};


template<size_t TruncSize, class Tup> struct th2;

//modifications to this class change "type" to the new truncated tuple type
//as well as modifying the template arguments to assign

template<size_t TruncSize, class Head, class... Tail>
struct th2<TruncSize, std::tuple<Head, Tail...>> 
{
    typedef typename tuple_trunc<TruncSize, std::tuple<Head, Tail...>>::type type;

    static type tail(const std::tuple<Head, Tail...>& tup) 
    {
        type t;
        assign<TruncSize, std::tuple_size<type>::value>::x(t, tup);
        return t;
    }
};

template<size_t TruncSize, class Tup>
typename th2<TruncSize, Tup>::type tail(const Tup& tup) 
{
    return th2<TruncSize, Tup>::tail(tup);
}

//a small example
int main()
{
    std::tuple<double, double, int, double> test(1, 2, 3, 4);
    tuple_trunc<2, std::tuple<double, double, int, double>>::type c = tail<2>(test);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Nik*_*iou 6

元组切片操作(即也适用于std::arraystd::pair)可以是这样的(所要求的C++ 14)来定义:

namespace detail
{
    template <std::size_t Ofst, class Tuple, std::size_t... I>
    constexpr auto slice_impl(Tuple&& t, std::index_sequence<I...>)
    {
        return std::forward_as_tuple(
            std::get<I + Ofst>(std::forward<Tuple>(t))...);
    }
}

template <std::size_t I1, std::size_t I2, class Cont>
constexpr auto tuple_slice(Cont&& t)
{
    static_assert(I2 >= I1, "invalid slice");
    static_assert(std::tuple_size<std::decay_t<Cont>>::value >= I2, 
        "slice index out of bounds");

    return detail::slice_impl<I1>(std::forward<Cont>(t),
        std::make_index_sequence<I2 - I1>{});
}
Run Code Online (Sandbox Code Playgroud)

并且t可以像这样获得元组的任意子集:

tuple_slice<I1, I2>(t); 
Run Code Online (Sandbox Code Playgroud)

哪里[I1, I2)独家范围的子集和返回值是引用或根据输入的元组是一个左或分别右值的值的元组(透彻的阐述可以在我发现博客).