std::forward_as_tuple(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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