# std::forward_as_tuple

From cppreference.com
< cpp‎ | utility‎ | tuple

Defined in header `<tuple>` |  |
template< class... Types > tuple<Types&&...> forward_as_tuple( Types&&... args ); |  | (since C++11) (until C++14)
template< class... Types > constexpr tuple<Types&&...> forward_as_tuple( Types&&... args ); |  | (since C++14)

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.

### Parameters

args | - | zero or more arguments to construct the tuple from

### Return value

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

### Exceptions

`noexcept` specification:
noexcept

### Notes

If the arguments are temporaries, `forward_as_tuple` does not extend their lifetime; they have to be used before the end of the full expression.

### Example

Run this code

    #include <iostream>
    #include <map>
    #include <tuple>
    #include <string>

    int main()
    {
        std::map<int, std::string> m;

        m.emplace(std::piecewise_construct,
                  std::forward_as_tuple(10),
                  std::forward_as_tuple(20, 'a'));
        std::cout << "m[10] = " << m[10] << '\n';

        // The following is an error: it produces a
        // std::tuple<int&&, char&&> holding two dangling references.
        //
        // auto t = std::forward_as_tuple(20, 'a');
        // m.emplace(std::piecewise_construct, std::forward_as_tuple(10), t);
    }

Output:

    m[10] = aaaaaaaaaaaaaaaaaaaa

### See also

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)
apply (C++17) | calls a function with a tuple of arguments (function template)

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple/forward_as_tuple&oldid=91415"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template