std::tie
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <tuple> で定義
|
||
template< class... Types > tuple<Types&...> tie( Types&... args ) noexcept; |
(C++11以上) (C++14以上ではconstexpr) |
|
引数または std::ignore のインスタンスを指す左辺値参照のタプルを作成します。
引数
| args | - | タプルを構築するための0個以上の左辺値引数 |
戻り値
左辺値参照を格納する std::tuple オブジェクト。
実装例
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...);
}
|
ノート
std::tuple にはペアからの変換代入演算子があるため、 std::tie は std::pair を分解するためにも使うことができます。
bool result;
std::tie(std::ignore, result) = set.insert(value);
例
std::tie を使用して、構造体を辞書的に比較したり、タプルを分解したりすることができます。
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";
}
出力:
Value was inserted successfully
関連項目
引数の型によって定義される型の tuple オブジェクトを作成します (関数テンプレート) | |
転送参照の tuple を作成します (関数テンプレート) | |
任意の数のタプルを連結して新たな tuple を作成します (関数テンプレート) | |
tuple を tie で分解するときに要素をスキップするためのプレースホルダです (定数) |