std::tie
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
template< class... Types > std::tuple<Types&...> tie( Types&... args ) noexcept; |
( C++11) ( C++14) |
|
template< class... Types > constexpr std::tuple<Types&...> tie( Types&... args ) noexcept; |
( C++14) | |
tuple lvalue std::ignore.
| args | lvalue . |
std::tuple, lvalue.
template <typename... Args>
constexpr // C++14
std::tuple<Args&...> tie(Args&... args) noexcept {
return {args...};
}
|
std::tie std::pair, std::tuple pair:
bool result;
std::tie(std::ignore, result) = set.insert(value);
#include <iostream>
#include <string>
#include <set>
#include <tuple>
struct S {
int n;
std::string s;
float d;
bool operator<(const S& rhs) const
{
// n rhs.n,
// s rhs.s,
// d 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 LessThanComparable
S value{42, "", 3.14};
std::set<S>::iterator iter;
bool inserted;
// insert iter inserted
std::tie(iter, inserted) = set_of_s.insert(value);
assert(inserted);
// std::tie C++17: //
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);
// , ,
auto coordinates = [] { return std::tuple<char, short>(6, 9); };
// ...
std::tie(x, y) = coordinates(); //
assert(x == 6 and y == 9);
}
| (C++17) | |
(C++11) |
tuple , ( ) |
(C++11) |
tuple ( ) |
(C++11) |
tuple, ( ) |
(C++11) |
tuple tie () |