< cpp‎ | utility‎ | tuple
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
Three-way comparison
General utilities
Date and time
Function objects
Formatting library (C++20)
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Elementary string conversions

Defined in header <tuple>
template< class... Types >
tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept;
(since C++11)
(constexpr 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.


[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] 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.

[edit] Example

#include <iostream>
#include <map>
#include <tuple>
#include <string>
int main()
    std::map<int, std::string> m;
              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);


m[10] = aaaaaaaaaaaaaaaaaaaa

[edit] See also

creates a tuple object of the type defined by the argument types
(function template) [edit]
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template) [edit]
creates a tuple by concatenating any number of tuples
(function template) [edit]
calls a function with a tuple of arguments
(function template) [edit]