<tuple>

function template
<tuple>

std::forward_as_tuple

template<class... Types>  tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept;
template<class... Types>  constexpr tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept;
Forward as tuple
Constructs a tuple object with rvalue references to the elements in args suitable to be forwarded as argument to a function.

This function is designed to forward arguments, not to store its result in a named variable, since the returned object may contain references to temporary variables.

It is equivalent to:
1
2
3
4
5
template<class... Types>
  tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept
{
  return tuple<Types&&...>(std::forward<Types>(args)...);
}

Parameters

args
List of elements to be forwarded as a tuple object of references.

Return Value

A tuple object with rvalue references to args suitable to be forwarded as argument to a function.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// forward_as_tuple example
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::get, std::forward_as_tuple
#include <string>       // std::string

void print_pack (std::tuple<std::string&&,int&&> pack) {
  std::cout << std::get<0>(pack) << ", " << std::get<1>(pack) << '\n';
}

int main() {
  std::string str ("John");
  print_pack (std::forward_as_tuple(str+" Smith",25));
  print_pack (std::forward_as_tuple(str+" Daniels",22));
  return 0;
}

Output:
John Smith, 25
John Daniels, 22


Data races

None introduced by this call.
Notice though, that if any type in Types supports move semantics or is an lvalue reference, its corresponding args argument may be modified by a function that is forwarded the value returned by this function.

Exception safety

No-throw guarantee: this function never throws exceptions.

See also