From cppreference.com
template<class...Types>classtuple;(since C++11)Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of
.
If std::is_trivially_destructible<Ti>::value is true for every Ti in Types, the destructor of std::tuple is trivial.
If a program declares an
or
specialization of std::tuple, the program is ill-formed, no diagnostic required.
Template parameters
Types... - the types of the elements that the tuple stores. Empty list is supported. Member functions
constructs a new tuple
(public member function)
assigns the contents of one tuple to another
(public member function)
swaps the contents of two tuples
(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
of lvalue references or unpacks a tuple into individual objects
(function template)
(C++11)
creates a tuple of
(function template)
(C++11)
creates a tuple by concatenating any number of tuples
(function template)
(C++11)
tuple accesses specified element
(function template)
operator==operator!=operator<operator<=operator>operator>=operator<=>
(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
algorithm
(function template)
Helper concepts
Helper classes
(C++11)
obtains the size of a tuple
(class template specialization)
std::tuple_element<std::tuple>
(C++11)
obtains the type of the specified element
(class template specialization)
std::uses_allocator<std::tuple>
(C++11)
specializes the
type trait
(class template specialization)
std::basic_common_reference<tuple-like>
(C++23)
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++23)
formatting support for tuple
(class template specialization)
(C++11)
placeholder to skip an element when unpacking a tuple using
(constant)
Helper specializations
template<class...Ts>constexprboolenable_nonlocking_formatter_optimization<std::tuple<Ts...>>=(enable_nonlocking_formatter_optimization<Ts>&&...);(since C++23)This specialization of
std::enable_nonlocking_formatter_optimization
enables efficient implementation of
and
for printing a tuple object when each element type enables it.
(since C++17)
Notes
Until
(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 N4387returnstd::tuple<int,int>{1,-1};// Always worksreturnstd::make_tuple(1,-1);// Always works}Example
Run this code
#include<iostream>#include<stdexcept>#include<string>#include<tuple>std::tuple<double,char,std::string>get_student(intid){switch(id){case0:return{3.8,'A',"Lisa Simpson"};case1:return{2.9,'C',"Milhouse Van Houten"};case2:return{1.7,'D',"Ralph Wiggum"};case3:return{0.6,'F',"Bart Simpson"};}throwstd::invalid_argument("id");}intmain(){constautostudent0=get_student(0);std::cout<<"ID: 0, "<<"GPA: "<<std::get<0>(student0)<<", "<<"grade: "<<std::get<1>(student0)<<", "<<"name: "<<std::get<2>(student0)<<'\n';constautostudent1=get_student(1);std::cout<<"ID: 1, "<<"GPA: "<<std::get<double>(student1)<<", "<<"grade: "<<std::get<char>(student1)<<", "<<"name: "<<std::get<std::string>(student1)<<'\n';doublegpa2;chargrade2;std::stringname2;std::tie(gpa2,grade2,name2)=get_student(2);std::cout<<"ID: 2, "<<"GPA: "<<gpa2<<", "<<"grade: "<<grade2<<", "<<"name: "<<name2<<'\n';// C++17 structured binding:constauto[gpa3,grade3,name3]=get_student(3);std::cout<<"ID: 3, "<<"GPA: "<<gpa3<<", "<<"grade: "<<grade3<<", "<<"name: "<<name3<<'\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 ID: 3, GPA: 0.6, grade: F, name: Bart Simpson Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++11 triviality of the destructor of std::tuple was unspecified specified
C++11 a program could declare an explicit or
partial specialization of std::tuplethe program is ill-formed in this
case (no diagnostic required) References
C++23 standard (ISO/IEC 14882:2024):
22.4 Tuples [tuple]
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)