< cpp‎ | utility‎ | tuple
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Elementary string conversions
Defined in header <tuple>
template< class... Types >
std::tuple<Types&...> tie( Types&... args ) noexcept;
(since C++11)
(until C++14)
template< class... Types >
constexpr std::tuple<Types&...> tie( Types&... args ) noexcept;
(since C++14)

Creates a tuple of lvalue references to its arguments or instances of std::ignore.


[edit] Parameters

args - zero or more lvalue arguments to construct the tuple from.

[edit] Return value

A std::tuple object containing lvalue references.

[edit] Possible implementation

template <typename... Args>
constexpr // since C++14
std::tuple<Args&...> tie(Args&... args) noexcept
    return {args...};

[edit] Notes

std::tie may be used to unpack a std::pair because std::tuple has a converting assignment from pairs:

bool result;
std::tie(std::ignore, result) = set.insert(value);

[edit] Example

1) std::tie can be used to introduce lexicographical comparison to a struct or to unpack a tuple;
2) std::tie can work with structured bindings:

#include <cassert>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
struct S
    int n;
    std::string s;
    float d;
    bool operator<(const S& rhs) const
        // compares n to rhs.n,
        // then s to rhs.s,
        // then d to rhs.d
        return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d);
int main()
    // Lexicographical comparison demo: //
    std::set<S> set_of_s; // S is LessThanComparable
    S value{42, "Test", 3.14};
    std::set<S>::iterator iter;
    bool inserted;
    // unpacks the return value of insert into iter and inserted
    std::tie(iter, inserted) = set_of_s.insert(value);
    // std::tie may work together with C++17 structured bindings: //
    auto position = [](int w) { return std::tuple<int, int>{ 1 * w, 2 * w }; };
    auto [x, y] = position(1);
    assert(x == 1 and y == 2);
    // ...
    std::tie(x, y) = position(2); // reuse x, y with tie
    assert(x == 2 and y == 4);
    // sub-types that are returned by a callable may differ
    auto coordinates = [] { return std::tuple<char, short>(6, 9); };
    // ...
    std::tie(x, y) = coordinates(); // implicit conversions
    assert(x == 6 and y == 9);

[edit] See also

Structured binding (C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
creates a tuple object of the type defined by the argument types
(function template) [edit]
creates a tuple of forwarding references
(function template) [edit]
creates a tuple by concatenating any number of tuples
(function template) [edit]
placeholder to skip an element when unpacking a tuple using tie
(constant) [edit]