function template

std::forward_as_tuple

<tuple>
template<class... Types>
  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>
#include <string>
#include <tuple>

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

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

See also

forward