std::forward_as_tuple

From Cppreference

Jump to: navigation, search
Defined in header <tuple>

template< class... Types >
tuple<Types...> forward_as_tuple( Types&&... args );
(C++11 feature)

Constructs a tuple of references to the arguments in args suitable for forwarding as an argument to a function. The tuple has rvalue reference data members when rvalues are used as arguments, and otherwise has lvalue reference data members. If rvalues are used, the result of this function must be consumed before the next sequence point.

Contents

[edit] Parameters

args - zero or more arguments to construct the tuple from

[edit] Return value

A std::tuple object created as if by std::tuple<Types&&...>(std::forward<Types>(args)...)

[edit] Exceptions

noexcept specification:  
noexcept

  (C++11 feature)

[edit] Example

#include <iostream>
#include <map>
#include <tuple>
#include <string>
 
int main()
{
    std::map<int, std::string> m;
 
    // same as m.emplace(10, 20, 'a');
    m.emplace(std::forward_as_tuple(10, std::string(20, 'a')));
    std::cout << "m[10] = " << m[10] << '\n';
 
    // The following is an error: it produces a
    // std::tuple<int&&, std::string&&> holding two dangling references.
    //
    // auto t = std::forward_as_tuple(10, std::string(20, 'a'));
    // m.emplace(t);
}

Output:

m[10] = aaaaaaaaaaaaaaaaaaaa
make_tuple
creates a tuple object of the type defined by the argument types
(function template)
tie
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template)
tuple_cat
creates a tuple by concatenating any number of tuples
(function template)