Namespaces
Variants

std:: forward_as_tuple

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

인수들을 함수에 인수로 전달하기에 적합한 참조들의 튜플을 생성합니다. 튜플은 rvalue가 인수로 사용될 때 rvalue 참조 데이터 멤버를 가지며, 그렇지 않은 경우 lvalue 참조 데이터 멤버를 가집니다.

목차

매개변수

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

반환값

마치 std::tuple 객체가 다음과 같이 생성된 것처럼: std:: tuple < Types && ... > ( std:: forward < Types > ( args ) ... )

참고 사항

인수가 임시 객체인 경우, forward_as_tuple 은 해당 객체의 수명을 연장하지 않습니다; 전체 표현식이 끝나기 전에 사용되어야 합니다.

예제

#include <iostream>
#include <map>
#include <string>
#include <tuple>
int main()
{
    std::map<int, std::string> m;
    m.emplace(std::piecewise_construct,
              std::forward_as_tuple(6),
              std::forward_as_tuple(9, 'g'));
    std::cout << "m[6] = " << m[6] << '\n';
    // 다음은 오류입니다: 두 개의 매달린 참조를 보유하는
    // std::tuple<int&&, char&&>을 생성합니다.
    //
    // auto t = std::forward_as_tuple(20, 'a');
    // m.emplace(std::piecewise_construct, std::forward_as_tuple(10), t);
}

출력:

m[6] = ggggggggg

참고 항목

(C++11)
인자 타입들로 정의된 타입의 tuple 객체를 생성함
(함수 템플릿)
(C++11)
lvalue 참조들의 tuple 을 생성하거나 tuple을 개별 객체들로 풀어냄
(함수 템플릿)
(C++11)
임의의 개수의 tuple들을 연결하여 tuple 을 생성함
(함수 템플릿)
(C++17)
인자들의 tuple로 함수를 호출함
(함수 템플릿)