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
154
votes
24 answers
How can you iterate over the elements of an std::tuple?
How can I iterate over a tuple (using C++11)? I tried the following:
for(int i=0; i::value; ++i)
std::get(my_tuple).do_sth();
but this doesn't work:
Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a…
1521237
131
votes
6 answers
Difference between std::pair and std::tuple with only two members?
Is there a difference between an std::pair and an std::tuple with only two members? (Besides the obvious that std::pair requires two and only two members and tuple may have more or less...)

Casey
- 10,297
- 11
- 59
- 88
52
votes
3 answers
C++ std::tuple order of destruction
Is there a rule which states in which order the members of an std::tuple are destroyed?
For example if Function1 returns an std::tuple, std::unique_ptr> to Function2, then can I be sure that (when the scope of…

z32a7ul
- 3,695
- 3
- 21
- 45
47
votes
2 answers
Why is std::pair faster than std::tuple
Here is the code for testing.
Tuple test:
using namespace std;
int main(){
vector> v;
for (int var = 0; var < 100000000; ++var) {
v.push_back(make_tuple(var, var));
}
}
Pair test:
#include
using…
user4233758
45
votes
3 answers
More silent behaviour changes with C++20 three-way comparison
To my surprise, I ran into another snag like C++20 behaviour breaking existing code with equality operator?.
Consider a simple case-insensitive key type, to be used with, e.g., std::set or std::map:
// Represents case insensitive keys
struct CiKey :…

sehe
- 374,641
- 47
- 450
- 633
40
votes
4 answers
What is the reason for `std::make_tuple`?
I mean why does std::make_tuple exist? I know that there are situations where the function reduces the amount of characters you have to type because you can avoid template parameters. But is it the only reason? What makes std::tuple special that the…

JojOatXGME
- 3,023
- 2
- 25
- 41
39
votes
5 answers
STL-pair-like triplet class - do I roll my own?
I want to use a triplet class, as similar as possible to std::pair. STL doesn't seem to have one. I don't want to use something too heavy, like Boost. Is there some useful FOSS non-restrictive-license triplet class I could lift from somewhere?…

einpoklum
- 118,144
- 57
- 340
- 684
32
votes
3 answers
What's the purpose of const swap() function?
While implementing a custom tuple (here), I found there is a wired swap() function that takes const parameters (cppreference):
template< class... Types >
constexpr void swap( const std::tuple& lhs,
const…

El Mismo Sol
- 869
- 1
- 6
- 14
31
votes
2 answers
Is using `std::get` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?
Let's say I have
std::tuple my_tuple{x0, x1, x2};
where T0, T1 and T2 are value types (i.e. no aliasing is possible).
Is it safe to access my_tuple's elements and mutate them concurrently from multiple threads using std::get, as long…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
31
votes
8 answers
Using tuple in unordered_map
I want to use tuple consisting of int,char,char in my unordered_map. I am doing like this:
#include
#include
#include
#include
#include
using namespace std;
tuple …

Xara
- 8,748
- 16
- 52
- 82
29
votes
2 answers
Why does std::tuple break small-size struct calling convention optimization in C++?
C++ has a small-size struct calling convention optimization where the compiler passes a small-size struct in function parameters as efficiently as it passes a primitive type (say, via registers). For example:
class MyInt { int n; public: MyInt(int…

YumeYao
- 557
- 4
- 10
29
votes
5 answers
How to reduce boilerplate currently necessary for serialization
Our software is abstracting away hardware, and we have classes that represent this hardware's state and have lots of data members for all properties of that external hardware. We need to regularly update other components about that state, and for…

sbi
- 219,715
- 46
- 258
- 445
27
votes
2 answers
What's the best way to return a tuple from function in C++11?
I want to return some values from a function and I want to pack it in a tuple. So I have two possibilities for function declaration:
std::tuple f()
{
...
return std::make_tuple(false, "home", 0);
}
and
std::tuple

Gian Lorenzo Meocci
- 1,136
- 2
- 14
- 23
27
votes
5 answers
Removing the first type of a std::tuple
This seems to be a very simple question: How does one remove the first (the n-th) type in a std::tuple?
Example:
typedef std::tuple tuple1;
typedef std::tuple tuple2;
The operation described above would transform…

cschwan
- 3,283
- 3
- 22
- 32
26
votes
3 answers
Uniform initialization by tuple
Today, I arrived at a situation, where I have a vector of tuples, where the tuples might contain several entries. Now I wanted to convert my vector of tuples to a vector of objects, such that the entries of the tuples will exactly match the uniform…

Aleph0
- 5,816
- 4
- 29
- 80