I have a tuple of unknown size (it's template parametr of method)
Is it way to get part of it (I need throw away first element of it)
For example, I have tuple<int,int,int>(7,12,42)
. I want tuple<int,int>(12,42)
here
I have a tuple of unknown size (it's template parametr of method)
Is it way to get part of it (I need throw away first element of it)
For example, I have tuple<int,int,int>(7,12,42)
. I want tuple<int,int>(12,42)
here
I made some modifications to Adam's code that would strip off the first N arguments of the tuple, as well as create a new tuple with only the last N types ... Here is the complete code (note: if anyone decides to +1 my answer, also please +1 Adam's answer since that is what this code is based on, and I don't wish to take any credit away from his contribution):
With C++17, you can use
std::apply
:Please don't use!
int
but may fail for your type!).See comments for discussion. I'm leaving this answer just for reference.
Even simpler:
I got:
I'm not certain that this is not an unspecified behaviour. Works for me: Fedora 20 and
References: article on voidnish.wordpress.com/.
There may be an easier way, but this is a start. The "tail" function template returns a copied tuple with all values of the original except the first. This compiles with GCC 4.6.2 in C++0x-mode.
With help of a compile-time integer list:
We could construct the tail simply by parameter-pack expansion:
Usage:
(On ideone: http://ideone.com/Tzv7v; the code works in g++ 4.5 to 4.7 and clang++ 3.0)
A tuple slice operation (that also works for
std::array
andstd::pair
) can be defined like this (C++14 required):And an arbitrary subset of a tuple
t
can be obtained like so :Where
[I1, I2)
is the exclusive range of the subset and the return value is a tuple of either references or values depending on whether the input tuple is an lvalue or an rvalue respectively (a thorough elaboration can be found in my blog).