tuple_make_tuple

tuple_make_tuple (튜플 생성 함수)

이 페이지는 C++ 표준 라이브러리의 std::make_tuple 함수 템플릿을 설명해요. make_tuple은 전달받은 인자들로부터 std::tuple 객체를 생성하며, 각 인자의 타입을 자동으로 추론해요. 특히 std::reference_wrapper가 인자로 전달되면 해당 타입은 참조 타입으로 변환되어 튜플에 저장돼요.

출처: cppreference

본문

<tuple> 헤더에 정의되어 있어요. (C++11부터 사용 가능하며, C++14부터 constexpr이에요.)

template < class ... Types > std :: tuple < VTypes ... > make_tuple ( Types && ... args );

make_tuple은 인자들의 타입으로부터 대상 타입을 추론하여 tuple 객체를 생성해요.
Types...의 각 Ti에 대해, VTypes...의 대응 타입 Vistd::decay<Ti>::type이에요. 단, std::decay를 적용한 결과가 어떤 타입 X에 대해 std::reference_wrapper<X>가 된다면, 그때 추론되는 타입은 X&가 돼요.

매개변수

args - 튜플을 생성하는 데 사용할 0개 이상의 인자들

반환값

주어진 값들을 담은 std::tuple 객체를 반환해요. 이 객체는 std::tuple<VTypes...>(std::forward<Types>(t)...)로 생성된 것과 동일해요.

가능한 구현

template < class T > struct unwrap_refwrapper { using type = T ; }; template < class T > struct unwrap_refwrapper < std :: reference_wrapper < T >> { using type = T & ; }; template < class T > using unwrap_decay_t = typename unwrap_refwrapper < typename std :: decay < T >:: type >:: type ; // or use std::unwrap_ref_decay_t (since C++20) template < class ... Types > constexpr // since C++14 std :: tuple < unwrap_decay_t < Types > ... > make_tuple ( Types && ... args ) { return std :: tuple < unwrap_decay_t < Types > ... > ( std :: forward < Types > ( args )...); }

예제

다음 예제는 make_tuple의 사용법을 보여줘요.

#include <iostream>
#include <tuple>
#include <functional>

std::tuple<int, int> f() // this function returns multiple values
{
    int x = 5;
    return std::make_tuple(x, 7); // return {x,7}; in C++17
}

int main()
{
    // heterogeneous tuple construction
    int n = 1;
    auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
    n = 7;
    std::cout << "The value of t is ("
              << std::get<0>(t) << ", "
              << std::get<1>(t) << ", "
              << std::get<2>(t) << ", "
              << std::get<3>(t) << ", "
              << std::get<4>(t) << ")\n";

    // function returning multiple values
    int a, b;
    std::tie(a, b) = f();
    std::cout << a << ' ' << b << '\n';
}

출력:

The value of t is (10, Test, 3.14, 7, 1)
5 7

같이 보기

tie (C++11) lvalue 참조들의 튜플을 생성하거나 튜플을 개별 객체로 분해해요 (함수 템플릿)
forward_as_tuple (C++11) 전달 참조들의 튜플을 생성해요 (함수 템플릿)
tuple_cat (C++11) 임의 개수의 튜플들을 연결하여 튜플을 생성해요 (함수 템플릿)
apply (C++17) 튜플의 인자들로 함수를 호출해요 (함수 템플릿)

더 알아보기 (Learn more)

cppreference