std::forward_as_tuple
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::tuple
forward_as_tuple
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++23)
(C++23)
(C++23)
Deduction guides (C++17)
Defined in header
<tuple>
template< class... Types >
std::tuple <Types&&...> forward_as_tuple( Types&&... args ) noexcept;
(since C++11) std::tuple <Types&&...> forward_as_tuple( Types&&... args ) noexcept;
(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.
Contents
[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
Run this code
#include <iostream> #include <map> #include <string> #include <tuple> int main() { std::map <int, std::string > m; m.emplace(std::piecewise_construct, std::forward_as_tuple(6), std::forward_as_tuple(9, 'g')); std::cout << "m[6] = " << m[6] << '\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[6] = ggggggggg