Подтвердить что ты не робот

Получить часть std:: tuple

У меня есть кортеж неизвестного размера (это шаблонный параметр метода)

Это способ получить часть его (мне нужно выбросить первый его элемент)

Например, у меня есть tuple<int,int,int>(7,12,42). Я хочу tuple<int,int>(12,42) здесь

4b9b3361

Ответ 1

С помощью целочисленного списка времени компиляции:

#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;
};

Мы могли бы построить хвост просто путем расширения пакета параметров:

#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, ..)
}

Использование:

#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;
}

(On ideone: http://ideone.com/Tzv7v, код работает в g ​​++ 4.5 до 4.7 и clang++ 3.0)

Ответ 2

Это может быть более простой способ, но это начало. Шаблон функции "хвост" возвращает скопированный кортеж со всеми значениями оригинала, кроме первого. Это компилируется с GCC 4.6.2 в режиме С++ 0x.

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

Ответ 3

Я внесла некоторые изменения в код Адама, который бы отменил первые N аргументов кортежа, а также создал новый кортеж только с последними N типами... Вот полный код (обратите внимание: если кто-то решит +1 мой ответ, пожалуйста, +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 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;
}

Ответ 4

Операция tuple slice (которая также работает для std::array и std::pair) может быть определена следующим образом (требуется С++ 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>{});
}

А произвольное подмножество кортежа t может быть получено так:

tuple_slice<I1, I2>(t); 

Где [I1, I2) - это эксклюзивный диапазон подмножества, а возвращаемое значение является кортежем как ссылок, так и значений в зависимости от того, является ли входной кортеж значением lvalue или rvalue соответственно (тщательная проработка можно найти в моем блоге ).

Ответ 5

С С++ 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);
}

Ответ 6

Пожалуйста, не используйте!

  • Это [наиболее вероятно] неуказанное поведение. Он может перестать работать в любое время.
  • Кроме того, есть возможность проблем с заполнением (т.е. может работать для int, но может не работать для вашего типа!).

См. комментарии для обсуждения. Я оставляю этот ответ только для справки.


Еще проще:

tuple<int,int,int> origin{7,12,42};
tuple<int, int> &tail1 = (tuple<int, int>&)origin;
tuple<int> &tail2 = (tuple<int>&)origin;
cout << "tail1: {" << get<0>(tail1) << ", " << get<1>(tail1) << "}" << endl;
cout << "tail2: {" << get<0>(tail2) << "}" << endl;

Я получил:

tail1: {12, 42}
tail2: {42}

Я не уверен, что это не неуказанное поведение. Работает для меня: Fedora 20 и

❯ clang --version
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
❯ gcc --version
gcc (GCC) 4.8.2 20131212 (Red Hat 4.8.2-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Ссылки: статья о voidnish.wordpress.com/.