C++ Tuple Library - forward_as_tuple


Advertisements

Description

It constructs a tuple object with rvalue references to the elements in args suitable to be forwarded as argument to a function.

Declaration

Following is the declaration for std::forward_as_tuple.

C++98

	
template<class... Types>
   tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept;

C++11

template<class... Types>
   tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept;

C++14

template<class... Types>
   constexpr tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept;

Parameters

args − It contains list of elements that the constructed tuple shall contain.

Return Value

It returns a tuple object of the appropriate type to hold args.

Exceptions

No-throw guarantee − this member function never throws exceptions.

Data races

None introduced by this call.

Example

In below example for std::forward_as_tuple.

#include <iostream>
#include <tuple>
#include <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 ("Tutorialspoint.com");
   print_pack (std::forward_as_tuple(str+" sairamkrishna",25));
   print_pack (std::forward_as_tuple(str+" Gopal",22));
   print_pack (std::forward_as_tuple(str+" Ram",30));
   return 0;
}

The output should be like this −

Tutorialspoint.com sairamkrishna, 25
Tutorialspoint.com Gopal, 22
Tutorialspoint.com Ram, 30
tuple.htm
Advertisements