std::forward_as_tuple(3)
π std::forward_as_tuple(3) π
okay bestie, welcome to the std::forward_as_tuple(3) page from the C++ Standard Libary manual π yes, that's the manual name folded in near the top, we're doing this cute and correct.
πΈ NAME πΈ
std::forward_as_tuple β std::forward_as_tuple.
That's literally the NAME line, she's minimal and iconic π
β¨ Synopsis β¨
Defined in header <tuple> π
template< class... Types > (since C++11)
tuple<Types&&...> forward_as_tuple( Types&&... args )
(constexpr since C++14)
noexcept;
Bestie, what this does: it constructs a tuple of references to the arguments in args, and that tuple is suitable for forwarding as an argument to a function. The tuple has rvalue reference data members when rvalues are used as arguments, and otherwise it has lvalue reference data members. No cap β¨
π Parameters π
argsβ zero or more arguments to construct the tuple from π«Ά
πΈ Return value πΈ
A std::tuple object created as if by:
std::tuple<Types&&...>(std::forward<Types>(args)...)
That's the whole return-value tea ββ¨
π¦ 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. Fr fr, do not let them dangle.
π Example π
// Run this code
#include <iostream>
#include <map>
#include <tuple>
#include <string>
int main()
{
std::map<int, std::string> m;
m.emplace(std::piecewise_construct,
std::forward_as_tuple(10),
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);
}
π Output π
m[10] = aaaaaaaaaaaaaaaaaaaa
π See also π
make_tupleβ creates a tuple object of the type defined by the argument types. (C++11) (function template)tieβ creates a tuple of lvalue references or unpacks a tuple into individual objects. (C++11) (function template)tuple_catβ creates a tuple by concatenating any number of tuples. (C++11) (function template)applyβ calls a function with a tuple of arguments. (C++17) (function template)
That's the whole page, bestie π«Ά β source: http://cppreference.com, version date: 2024.06.10, page: std::forward_as_tuple(3). Byeee πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms