std::tie (3) - Linux Manuals
std::tie: std::tie
Command to display std::tie
manual in Linux: $ man 3 std::tie
NAME
std::tie - std::tie
Synopsis
Defined in header <tuple>
template< class... Types > (since C++11)
tuple<Types&...> tie( Types&... args ) noexcept; (constexpr since C++14)
Creates a tuple of lvalue references to its arguments or instances of std::ignore.
Parameters
args - zero or more lvalue arguments to construct the tuple from
Return value
A std::tuple object containing lvalue references.
Possible implementation
namespace detail {
struct ignore_t {
template <typename T>
const ignore_t& operator=(const T&) const { return *this; }
};
}
const detail::ignore_t ignore;
template <typename... Args>
auto tie(Args&... args) {
return std::tuple<Args&...>(args...);
}
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);
Example
std::tie can be used to introduce lexicographical comparison to a struct or to unpack a tuple:
// Run this code
#include <iostream>
#include <string>
#include <set>
#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()
{
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);
if (inserted)
std::cout << "Value was inserted successfully\n";
}
Output:
Value was inserted successfully
See also
creates a tuple object of the type defined by the argument types
make_tuple (function template)
creates a tuple of rvalue references
forward_as_tuple (function template)
creates a tuple by concatenating any number of tuples
tuple_cat (function template)
placeholder to skip an element when unpacking a tuple using tie
ignore (constant)
Pages related to std::tie
- std::time (3) - std::time
- std::time_base (3) - std::time_base
- std::time_get (3) - std::time_get
- std::time_get<CharT,InputIt>::date_order,std::time_get<CharT,InputIt>::do_date_order (3) - std::time_get<CharT,InputIt>::date_order,std::time_get<CharT,InputIt>::do_date_order
- std::time_get<CharT,InputIt>::get,std::time_get<CharT,InputIt>::do_get (3) - std::time_get<CharT,InputIt>::get,std::time_get<CharT,InputIt>::do_get
- std::time_get<CharT,InputIt>::get_date,std::time_get<CharT,InputIt>::do_get_date (3) - std::time_get<CharT,InputIt>::get_date,std::time_get<CharT,InputIt>::do_get_date
- std::time_get<CharT,InputIt>::get_monthname, (3) - std::time_get<CharT,InputIt>::get_monthname,
- std::time_get<CharT,InputIt>::get_monthname,std::time_get<CharT,InputIt>::do_get_monthname (3) - std::time_get<CharT,InputIt>::get_monthname,std::time_get<CharT,InputIt>::do_get_monthname
- std::time_get<CharT,InputIt>::get_time,std::time_get<CharT,InputIt>::do_get_time (3) - std::time_get<CharT,InputIt>::get_time,std::time_get<CharT,InputIt>::do_get_time
- std::time_get<CharT,InputIt>::get_weekday,std::time_get<CharT,InputIt>::do_get_weekday (3) - std::time_get<CharT,InputIt>::get_weekday,std::time_get<CharT,InputIt>::do_get_weekday
- std::time_get<CharT,InputIt>::get_year,std::time_get<CharT,InputIt>::do_get_year (3) - std::time_get<CharT,InputIt>::get_year,std::time_get<CharT,InputIt>::do_get_year