名前空間
変種
操作

std::tie

提供: cppreference.com
< cpp‎ | utility‎ | tuple
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
std::tuple
メンバ関数
非メンバ関数
tie
(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20)
推定ガイド(C++17)
ヘルパークラス
 
ヘッダ <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::tiestd::pair を分解するためにも使うことができます。

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

[編集]

std::tie を使用して、構造体を辞書的に比較したり、タプルを分解したりすることができます。

#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 オブジェクトを作成します
(関数テンプレート) [edit]
転送参照tuple を作成します
(関数テンプレート) [edit]
任意の数のタプルを連結して新たな tuple を作成します
(関数テンプレート) [edit]
tupletie で分解するときに要素をスキップするためのプレースホルダです
(定数) [edit]