What std::tuple is
A std::tuple holds a fixed number of values, and each value can have its own type. It lives in the <tuple> header and was added to the standard library in C++11. A tuple such as std::tuple<std::string, int> holds one string and one int. The number of elements and their types are part of the tuple’s type, so they are fixed when the code is compiled.
If the question “What is the definition of a tuple?” is new to you, start there for the general idea. This article focuses on how C++ implements it.
Creating a tuple
List the element types in angle brackets, then the values in braces:
#include <iostream>
#include <string>
#include <tuple>
int main() {
std::tuple<std::string, int, double> person{"Ada", 36, 1.72};
std::cout << std::get<0>(person) << ", "
<< std::get<1>(person) << ", "
<< std::get<2>(person) << '\n';
}
The program prints:
Ada, 36, 1.72
The number inside std::get<...> is the position of the element, counting from 0.
Creating a tuple with std::make_tuple
std::make_tuple builds a tuple from values, so you don’t have to write the types out:
auto scores = std::make_tuple("Ada", 36);
The first element here has type const char*, not std::string, because std::make_tuple turns a string literal into a const char*. If you want a std::string, wrap the value yourself: std::make_tuple(std::string("Ada"), 36).
Reading and changing elements with std::get
std::get<I>(t) returns the element at index I. Two rules matter:
- The index must be a constant known at compile time. An index past the last element, such as
std::get<3>on a three-element tuple, fails to compile. - Since C++14, you can ask for an element by its type with
std::get<T>(t). This works only when that type appears exactly once in the tuple.
std::get returns a reference, so you can assign through it:
std::get<1>(person) = 37; // Ada is now 37
Unpacking with std::tie and structured bindings
std::tie makes a tuple of references to existing variables. Assigning a tuple to it copies the values into those variables:
std::string name;
int age;
double height;
std::tie(name, age, height) = person;
Use std::ignore to skip an element you don’t need:
std::tie(name, age, std::ignore) = person;
Since C++17, structured bindings do the same job and declare the variables in one line:
auto [name, age, height] = person;
Returning several values from a function
A function can return a tuple when it has more than one result to give back:
#include <tuple>
std::tuple<int, int> divide_with_remainder(int a, int b) {
return std::make_tuple(a / b, a % b);
}
The caller can unpack the result with a structured binding:
auto [quotient, remainder] = divide_with_remainder(17, 5);
// quotient is 3 and remainder is 2
Passing tuple elements to a function with std::apply
Since C++17, std::apply calls a function and passes each element of a tuple as a separate argument:
#include <iostream>
#include <string>
#include <tuple>
void print_person(const std::string& name, int age) {
std::cout << name << " is " << age << '\n';
}
int main() {
auto person_info = std::make_tuple(std::string("Ada"), 36);
std::apply(print_person, person_info);
}
The program prints Ada is 36.
Comparing and joining tuples
Two tuples of the same size can be compared with ==, < and the other comparison operators. The comparison goes element by element, from the first element to the last.
std::tuple_cat joins tuples into one:
auto combined = std::tuple_cat(std::make_tuple(1, 'a'), std::make_tuple(2.5));
// combined is std::tuple<int, char, double>
Tuple, pair or struct?
std::pair, from <utility>, is a tuple that always holds exactly two elements, reached as first and second. Use it when you have exactly two values.
A tuple’s elements have no names, so std::get<1> says nothing about what the value means. When the values have a clear meaning, such as a person’s name and age, a struct with named members is often easier to read. If you’re weighing the two, see “Should you use struct in C++?”
Tuples suit short-lived groups of values, generic code, and functions that return several results.
Common mistakes
Watch for these when you start using tuples:
- Forgetting
#include <tuple>before usingstd::tuple. Include it explicitly rather than relying on another header to bring it in. - Printing a tuple with
std::cout << person. The standard library has no output operator for tuples, so print each element withstd::get, as shown above. - Using an index past the end, such as
std::get<3>(person)on a three-element tuple. The compiler rejects it. - Calling
std::get<int>(t)on a tuple with twointelements. The type is ambiguous, so the code does not compile.