std::make_tuple(3)
π std::make_tuple π
okay so welcome to the std::make_tuple(3) page from the C++ Standard Libary manual β yes, thatβs the manual name, weβre keeping it exactly, no cap. This is the one where C++ looks at your pile of arguments and goes βI gotchu, letβs make a tuple,β and deduces the target type from their types. slay. β¨
πΈ NAME πΈ
std::make_tuple β std::make_tuple
yeah bestie, the NAME section is literally that. iconic minimalism. π
β¨ Synopsis β¨
Defined in header <tuple>
template< class... Types > (since C++11)
std::tuple<VTypes...> make_tuple( Types&&... args ); (constexpr
since C++14)
Creates a tuple object, deducing the target type from the types of arguments.
For each Ti in Types..., the corresponding type Vi in VTypes... is std::decay<Ti>::type β unless application of std::decay results in std::reference_wrapper<X> for some type X, in which case the deduced type is X&. the way this deduction logic is doing the absolute most and still serving exactly what it needs to serve. π¦
π Parameters π
argsβ zero or more arguments to construct the tuple from.
π«Ά Return value π«Ά
A std::tuple object containing the given values, created as if by std::tuple<VTypes...>(std::forward<Types>(t)...). periodt. β¨
π§ Possible implementation π§
hereβs a possible implementation, and yes itβs giving template wizardry:
template <class T>
struct unwrap_refwrapper
{
using type = T;
};
template <class T>
struct unwrap_refwrapper<std::reference_wrapper<T>>
{
using type = T&;
};
template <class T>
using unwrap_decay_t = typename unwrap_refwrapper<typename
std::decay<T>::type>::type;
// or use std::unwrap_ref_decay_t (since C++20)
template <class... Types>
constexpr // since C++14
std::tuple<unwrap_decay_t<Types>...> make_tuple(Types&&... args)
{
return
std::tuple<unwrap_decay_t<Types>...>(std::forward<Types>(args)...);
}
π Example π
// Run this code β hereβs the whole example, fr fr:
#include <iostream>
#include <tuple>
#include <functional>
std::tuple<int, int> f() // this function returns multiple values
{
int x = 5;
return std::make_tuple(x, 7); // return {x,7}; in C++17
}
int main()
{
// heterogeneous tuple construction
int n = 1;
auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
n = 7;
std::cout << "The value of t is ("
<< std::get<0>(t) << ", "
<< std::get<1>(t) << ", "
<< std::get<2>(t) << ", "
<< std::get<3>(t) << ", "
<< std::get<4>(t) << ")\n";
// function returning multiple values
int a, b;
std::tie(a, b) = f();
std::cout << a << ' ' << b << '\n';
}
π« Output π«
The value of t is (10, Test, 3.14, 7, 1)
5 7
π See also π
more tuple lore, iykyk: π
tieβ creates a tuple of lvalue references or unpacks a tuple into individual objects.(C++11)(function template)forward_as_tupleβ creates a tuple of forwarding references.(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)
mwah π thatβs the whole page, bestie. source is http://cppreference.com, version/date is 2024.06.10, and the page is std::make_tuple(3). go make some tuples and absolutely ate that template deduction. πβ¨
π¦ 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