std::tuple
From cppreference.com
Defined in header <tuple>
|
||
template< class... Types > class tuple; |
(since C++11) | |
Class template std::tuple
is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.
If std::is_trivially_destructible<Ti>::value is true for every Ti
in Types
, the destructor of tuple
is trivial.
Template parameters
Types... | - | the types of the elements that the tuple stores. Empty list is supported. |
Member functions
(C++11) |
constructs a new tuple (public member function) |
(C++11) |
assigns the contents of one tuple to another (public member function) |
(C++11) |
swaps the contents of two tuple s (public member function) |
Non-member functions
(C++11) |
creates a tuple object of the type defined by the argument types (function template) |
(C++11) |
creates a tuple of lvalue references or unpacks a tuple into individual objects (function template) |
(C++11) |
creates a tuple of forwarding references (function template) |
(C++11) |
creates a tuple by concatenating any number of tuples (function template) |
(C++11) |
tuple accesses specified element (function template) |
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) |
lexicographically compares the values in the tuple (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
Helper classes
(C++11) |
obtains the size of tuple at compile time (class template specialization) |
obtains the type of the specified element (class template specialization) | |
specializes the std::uses_allocator type trait (class template specialization) | |
determines the common reference type of a tuple and a tuple-like type (class template specialization) | |
(C++23) |
determines the common type of a tuple and a tuple-like type (class template specialization) |
(C++11) |
placeholder to skip an element when unpacking a tuple using tie (constant) |
Deduction guides (since C++17)
Notes
Until N4387 (applied as a defect report for C++11), a function could not return a tuple using copy-list-initialization:
std::tuple<int, int> foo_tuple() { return {1, -1}; // Error until N4387 return std::tuple<int, int>{1, -1}; // Always works return std::make_tuple(1, -1); // Always works }
Example
Run this code
#include <tuple> #include <iostream> #include <string> #include <stdexcept> std::tuple<double, char, std::string> get_student(int id) { if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson"); if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten"); if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum"); throw std::invalid_argument("id"); } int main() { auto student0 = get_student(0); std::cout << "ID: 0, " << "GPA: " << std::get<0>(student0) << ", " << "grade: " << std::get<1>(student0) << ", " << "name: " << std::get<2>(student0) << '\n'; double gpa1; char grade1; std::string name1; std::tie(gpa1, grade1, name1) = get_student(1); std::cout << "ID: 1, " << "GPA: " << gpa1 << ", " << "grade: " << grade1 << ", " << "name: " << name1 << '\n'; // C++17 structured binding: auto [ gpa2, grade2, name2 ] = get_student(2); std::cout << "ID: 2, " << "GPA: " << gpa2 << ", " << "grade: " << grade2 << ", " << "name: " << name2 << '\n'; }
Output:
ID: 0, GPA: 3.8, grade: A, name: Lisa Simpson ID: 1, GPA: 2.9, grade: C, name: Milhouse Van Houten ID: 2, GPA: 1.7, grade: D, name: Ralph Wiggum
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2796 | C++11 | triviality of the destructor of tuple was unspecified
|
specified |
References
- C++20 standard (ISO/IEC 14882:2020):
- 20.5 Tuples [tuple]
- C++17 standard (ISO/IEC 14882:2017):
- 23.5 Tuples [tuple]
- C++14 standard (ISO/IEC 14882:2014):
- 20.4 Tuples [tuple]
- C++11 standard (ISO/IEC 14882:2011):
- 20.4 Tuples [tuple]
See also
implements binary tuple, i.e. a pair of values (class template) |