Filesystem library (C++17) Regular expressions library (C++11) Concurrency support library (C++11) Technical specifications Symbols index External libraries
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Three-way comparison (C++20)
(C++20)
(C++20) (C++20) (C++20) (C++20) (C++20) (C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
tuple
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
Defined in header <tuple>

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.

Contents

  • 1 Template parameters
  • 2 Member functions
  • 3 Non-member functions
  • 4 Helper concepts
  • 5 Helper classes
  • 6 Deduction guides (since C++17)
  • 7 Notes
  • 8 Example
  • 9 Defect reports
  • 10 References
  • 11 See also
  • [ edit ] Template parameters

    (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) [edit]
    specializes the std::swap algorithm
    (function template) [edit]

    [ edit ] Helper concepts

    specifies that a type implemented the tuple protocol
    ( std::get , std::tuple_element , std::tuple_size )
    (exposition-only concept) [edit]

    [ edit ] Helper classes

    obtains the size of tuple at compile time
    (class template specialization) [edit]
    obtains the type of the specified element
    (class template specialization) [edit]
    specializes the std::uses_allocator type trait
    (class template specialization) [edit]
    determines the common reference type of a tuple and a tuple-like type
    (class template specialization) [edit]
    determines the common type of a tuple and a tuple-like type
    (class template specialization) [edit]
    (C++11)
    placeholder to skip an element when unpacking a tuple using tie
    (constant) [edit]

    [ edit ] Deduction guides (since C++17)

    [ edit ] 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
    

    [edit] Example

    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <tuple>
    std::tuple<double, char, std::string> get_student(int id)
        switch (id)
            case 0: return {3.8, 'A', "Lisa Simpson"};
            case 1: return {2.9, 'C', "Milhouse Van Houten"};
            case 2: return {1.7, 'D', "Ralph Wiggum"};
            case 3: return {0.6, 'F', "Bart Simpson"};
        throw std::invalid_argument("id");
    int main()
        const 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';
        const auto student1 = get_student(1);
        std::cout << "ID: 1, "
                  << "GPA: " << std::get<double>(student1) << ", "
                  << "grade: " << std::get<char>(student1) << ", "
                  << "name: " << std::get<std::string>(student1) << '\n';
        double gpa2;
        char grade2;
        std::string name2;
        std::tie(gpa2, grade2, name2) = get_student(2);
        std::cout << "ID: 2, "
                  << "GPA: " << gpa2 << ", "
                  << "grade: " << grade2 << ", "
                  << "name: " << name2 << '\n';
        // C++17 structured binding:
        const auto [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

    [edit] Defect reports

    The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

    Applied to Behavior as published Correct behavior LWG 2796 C++11 triviality of the destructor of tuple was unspecified specified

    [ edit ] References