1

Topic: To copy elements except for the last

Greetings! Who can already wrote a similar bicycle? To eliminate the first - it is simple. And with last something is impossible... It is necessary for std:: tuple, in which only constant links. That it was possible to use so: auto res = tuple_pop_back (src); thanks.

2

Re: To copy elements except for the last

Hello, niXman, you wrote: X> to eliminate the first - it is simple. And with last something is impossible... And in what a difference if to them on indexes to address? template <class T, size_t... I> auto slice_tuple (const T& t, std:: index_sequence I.>) {return std:: tuple <typename std:: tuple_element <I, T>:: type...> ((std:: get <I> (t))...);} template <class THead, class... TTail> auto tuple_pop_back (const std:: tuple <THead, TTail...>& t) {return slice_tuple (t, std:: make_index_sequence <sizeof... (TTail)> ());}

3

Re: To copy elements except for the last

Hello, watchmaker, you wrote: thanks W> And in what a difference if to them on indexes to address? And to indexes I had not time to finish thinking, I went on a forum started to think about the recursive copying and  last, but that  to unwinding did not earn that. %) in general - laziness

4

Re: To copy elements except for the last

Hello, niXman, you wrote: X> greetings! X> who can already wrote a similar bicycle? X> to eliminate the first - it is simple. And with last something is impossible... X> it is necessary for std:: tuple, in which only constant links. That it was possible to use so: auto res = tuple_pop_back (src); X> thanks. It is possible to begin from here http://en.cppreference.com/w/cpp/utilit … from_tuple and hardly to alter #include <tuple> namespace detail {template <class Tuple, std:: size_t... I> constexpr auto tuple_pop_back_impl (Tuple&& t, std:: index_sequence I.>) {return std:: make_tuple (std:: get <I> (std:: forward <Tuple> (t))...);}}//namespace detail template <class Tuple> constexpr auto tuple_pop_back (Tuple&& t) {return detail:: tuple_pop_back_impl (std:: forward <Tuple> (t), std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value-1> {});} int main () {std:: tuple <int, char> res = tuple_pop_back (std:: make_tuple (220, ' 2 ', 3.14)); return 0;}

5

Re: To copy elements except for the last

Hello, niXman, you wrote: X> to eliminate the first - it is simple. And with last something is impossible... In traditions of prompt replies on stackoverflow: to make  with  order of elements, to delete the first element, again  the order of elements

6

Re: To copy elements except for the last

Hello, Mr. Delphist, you wrote: MD> In traditions of prompt replies on stackoverflow: to make  with  order of elements, to delete the first element, again  the order of elements geniuses! I would not guess before smile

7

Re: To copy elements except for the last

Hello, watchmaker, you wrote: W> W> template <class T, size_t... I> W> auto slice_tuple (const T& t, std:: index_sequence I.>) {W> return std:: tuple <typename std:: tuple_element <I, T>:: type...> ((std:: get <I> (t))...); W>} W> template <class THead, class... TTail> W> auto tuple_pop_back (const std:: tuple <THead, TTail...>& t) {W> return slice_tuple (t, std:: make_index_sequence <sizeof... (TTail)> ()); W>} In a C ++ 20 it can be pushed in one function, used generic lambda with the list of sample parameters: #include <tuple> #include <utility> template <typename... Elements> constexpr auto tuple_pop_back (const std::tuple<Elements...>& tuple) noexcept {static_assert (sizeof... (Elements)> 0, "' tuple_pop_back ': ' tuple ' must be a non-empty tuple." ); return [&tuple] <std:: size_t... indices> (std:: index_sequence <indices...>) {return std:: tie (std:: get <indices> (tuple)...);} (std:: make_index_sequence <sizeof... (Elements) - 1> {});}