Namespaces
Variants

std:: tie

From cppreference.net
Utilities library
헤더 파일에 정의됨 <tuple>
template < class ... Types >
std:: tuple < Types & ... > tie ( Types & ... args ) noexcept ;
(C++11부터)
(C++14부터 constexpr)

인수들에 대한 좌측값 참조 또는 std::ignore 인스턴스들의 튜플을 생성합니다.

목차

매개변수

args - 튜플을 생성하기 위한 0개 이상의 lvalue 인수들.

반환값

lvalue 참조들을 포함하는 std::tuple 객체.

가능한 구현

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

참고 사항

std::tie std::pair 를 언패킹하는 데 사용될 수 있습니다. 왜냐하면 std::tuple 이 페어로부터의 변환 할당 을 지원하기 때문입니다:

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

예제

1) std::tie 는 구조체에 사전식 비교를 도입하거나 튜플을 언패킹하는 데 사용할 수 있습니다;
2) std::tie 구조화된 바인딩 과 함께 작동할 수 있습니다:

#include <cassert>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
struct S
{
    int n;
    std::string s;
    float d;
    friend bool operator<(const S& lhs, const S& rhs) noexcept
    {
        // lhs.n을 rhs.n과 비교하고,
        // lhs.s를 rhs.s와 비교하고,
        // lhs.d를 rhs.d와 비교합니다
        // 해당 순서로, 첫 번째로 다른 결과가 반환되거나
        // 모든 요소가 같으면 false가 반환됩니다
        return std::tie(lhs.n, lhs.s, lhs.d) < std::tie(rhs.n, rhs.s, rhs.d);
    }
};
int main()
{
    // 사전식 비교 데모:
    std::set<S> set_of_s;
    S value{42, "Test", 3.14};
    std::set<S>::iterator iter;
    bool is_inserted;
    // pair 언패킹:
    std::tie(iter, is_inserted) = set_of_s.insert(value);
    assert(is_inserted);
    // std::tie와 구조화된 바인딩:
    auto position = [](int w) { return std::tuple(1 * w, 2 * w); };
    auto [x, y] = position(1);
    assert(x == 1 && y == 2);
    std::tie(x, y) = position(2); // tie로 x, y 재사용
    assert(x == 2 && y == 4);
    // 암시적 변환이 허용됩니다:
    std::tuple<char, short> coordinates(6, 9);
    std::tie(x, y) = coordinates;
    assert(x == 6 && y == 9);
    // 요소 건너뛰기:
    std::string z;
    std::tie(x, std::ignore, z) = std::tuple(1, 2.0, "Test");
    assert(x == 1 && z == "Test");
}

참고 항목

Structured binding (C++17) 지정된 이름들을 초기화자의 부분 객체나 튜플 요소에 바인딩합니다
(C++11)
인자 타입들로 정의된 타입의 tuple 객체를 생성합니다
(function template)
전달 참조 들의 tuple 을 생성합니다
(function template)
(C++11)
임의의 개수의 튜플들을 연결하여 tuple 을 생성합니다
(function template)
(C++11)
tie 를 사용하여 tuple 을 언패킹할 때 요소를 건너뛰기 위한 플레이스홀더
(constant)