Use this tag for questions about the C++ standard library template std::tuple. It represents an ordered, heterogeneous sequence of objects. Also add the tag [c++] when using this tag.
Questions tagged [stdtuple]
403 questions
9
votes
3 answers
aliasing a std template function
I need to alias std::get function in order to improve readability in my code.
Unfortunately I got a compile-time error get<0> in namespace ‘std’ does not name a type. using is equivalent to typedef so it needs types to work with.
I am using a…

chedy najjar
- 631
- 7
- 19
9
votes
3 answers
How do I compare tuples for equivalent types disregarding type order?
I'm looking for a way to compare two tuples to see if they contain the same types.
The order of the types does not matter. As long as there is a one to one mapping between the types of the two tuples, I will consider them equivalent.
Here is a…

Trevor Hickey
- 36,288
- 32
- 162
- 271
9
votes
1 answer
Why can't you assign a pair from a tuple, but tuple can be assigned from a pair?
I'm not clear why it is legal to assign tuple=pair
But it is illegal to assign pair=tuple
std::pair x { 1 , 5.5};
std::tuple y { 1 , 5.5};
int a;
double b;
std::tie(a,b) = x;
…

Glenn Teitelbaum
- 10,108
- 3
- 36
- 80
8
votes
2 answers
Is the behaviour of std::get on rvalue-reference tuples dangerous?
The following code:
#include
int main ()
{
auto f = [] () -> decltype (auto)
{
return std::get<0> (std::make_tuple (0));
};
return f ();
}
(Silently) generates code with undefined behaviour - the temporary rvalue returned by…

Matt A
- 103
- 6
8
votes
4 answers
how to write a fold /sum function for C++ tuple?
I wanted to write a fold function for std::tuple that can compute e.g. the sum (or product) of all the elements in a given tuple. For example, given
std::tuple t = std::make_tuple(1,2);
I'd like to compute
auto s = sumT(t); //giving…

thor
- 21,418
- 31
- 87
- 173
8
votes
1 answer
Are implicit conversions allowed with std::tie?
In c++11, are implicit conversions allowed with std::tie?
The following code compiles and runs but I'm not sure exactly what's going on behind the scenes or if this is safe.
std::tuple foo() { return std::make_tuple(0,0); }
double a,…

lenguador
- 476
- 5
- 9
7
votes
4 answers
C++ std::get fails
How do I use a variable to index into a tuple using std::get<>? I have the following code:
#include
#include
using namespace std;
int main() {
tuple data(5, 10);
for (int i=0; i<2; i++) {
cout << "#" << i+1 <<…

ragnar
- 71
- 1
- 3
7
votes
1 answer
Calling `std::get` on `std::tuple` with elements deriving from `std::tuple` - ill-formed?
struct Y { };
struct X : std::tuple { };
int main()
{
std::get<0>(std::make_tuple(X{}));
}
on wandbox
The above code compiles and works as expected with clang++ when using libc++.
The above code fails to compile with both clang++ and g++…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
7
votes
3 answers
How can I check if all members of two tuples are different?
std::tuple<...>::operator!= returns true if at least one member of the two compared tuples is different.
I would need a function that would return true if all members of two compared tuples are different:
template
bool…

jpo38
- 20,821
- 10
- 70
- 151
7
votes
2 answers
Tuple isn't being constructed in order?
The following program:
#include
#include
struct A {
A() { std::cout << "A constructor\n"; }
};
struct B {
B() { std::cout << "B constructor\n"; }
};
int main() {
std::tuple t;
}
gives different outputs on…

Brian Rodriguez
- 4,250
- 1
- 16
- 37
7
votes
1 answer
Error when checking if a tuple of references is default constructible
With g++-5 I get the following output
#include
#include
int main()
{
bool b;
b = std::is_default_constructible::value; //Compiles, returns true
b = std::is_default_constructible::value; //Compiles, returns…

user3559888
- 1,157
- 1
- 9
- 12
7
votes
3 answers
Applying func to elements in std::tuple in the natural (not reverse) order
I need to call a - template or overloaded - function for each element in an arbitrary tuple. To be precise, I need to call this function on the elements as they are specified in the tuple.
For example. I have a tuple std::tuple t{1,…

Mischa
- 215
- 1
- 9
7
votes
2 answers
Are all the std::tuple constructors necessary?
std::tuple contains, amongst others, the following constructors:
explicit tuple( const Types&... args );
template< class... UTypes >
explicit tuple( UTypes&&... args );
Both have equivalent descriptions in that they initialise each of the elements…

DrYap
- 6,525
- 2
- 31
- 54
7
votes
3 answers
How to iterate over a TR1 tuple
Being stuck in TR1 land, for a test program I need to perform certain operations on a number of objects of specific types. I have a couple of tuple type definitions which look like this:
typedef std::tr1::tuple< bool
,…

sbi
- 219,715
- 46
- 258
- 445
6
votes
1 answer
How to construct a tuple with four elements from two pairs with matching data types in C++?
I have a function f(), that returns a std::pair with some types A and B. And I have another function g(), that calls f() twice and returns a std::tuple. Is there a way to construct the return tuple directly from both calls to f()?…

Kai Petzke
- 2,150
- 21
- 29