std::make_from_tuple(3)
π NAME π
Okay bestie, welcome to the std::make_from_tuple(3) page from the C++ Standard Libary π«Ά. The name is literally:
std::make_from_tuple - std::make_from_tuple
Yeah, it's giving "the function is the name and the name is the function" β¨.
πΈ Synopsis πΈ
First, the vibe check: it lives in header <tuple>.
Defined in header <tuple>
template< class T, class Tuple > (since C++17)
constexpr T make_from_tuple( Tuple&& t ); (until C++23)
template< class T, tuple-like Tuple > (since C++23)
constexpr T make_from_tuple( Tuple&& t );
Construct an object of type T, using the elements of the tuple t as the arguments to the constructor. Slay.
Given the exposition-only function /*make-from-tuple-impl*/ defined as follows:
template<class T, tuple-like Tuple, std::size_t... I> // no constraint on Tuple before C++23
constexpr T /*make-from-tuple-impl*/(Tuple&& t, std::index_sequence<I...>)
{
return T(std::get<I>(std::forward<Tuple>(t))...);
}
The effect is equivalent to:
return /*make-from-tuple-impl*/<T>(
std::forward<Tuple>(t),
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{}
);.
If either of these is true, the program is ill-formed π¨:
std::tuple_size_v<std::remove_reference_t<Tuple>>is1and (since C++23)std::reference_constructs_from_temporary_v<T, decltype(std::get<0>(std::declval<Tuple>()))>istrue, orstd::is_constructible_v<T, decltype(std::get<I>(std::declval<Tuple>()))...>isfalse.
π Parameters π
tβ tuple whose elements to be used as arguments to the constructor ofT.
π¦ Return value π¦
The constructed T object or reference. That's it, bestie.
π Notes π
Tupleneed not bestd::tuple, and instead may be anything that supportsstd::getandstd::tuple_size; in particular,std::arrayand (until C++23)std::pairmay be used.Tupleis constrained to be tuple-like, i.e. each type therein is required to be a specialization ofstd::tupleor another type (such as (since C++23)std::arrayandstd::pair) that models tuple-like.- Due to guaranteed copy elision,
Tneed not be movable. Iconic.
Feature-test macro vibes:
Feature-test macro Value Std Feature
__cpp_lib_make_from_tuple 201606L (C++17) std::make_from_tuple
π Example π
// Run this code
#include <iostream>
#include <tuple>
struct Foo
{
Foo(int first, float second, int third)
{
std::cout << first << ", " << second << ", " << third << '\n';
}
};
int main()
{
auto tuple = std::make_tuple(42, 3.14f, 0);
std::make_from_tuple<Foo>(std::move(tuple));
}
β Output β
42, 3.14, 0
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 3528- Applied to:
C++17 - Behavior as published: cast containing
reinterpret_castetc. was prohibited - Correct behavior: allowed in the case of 1-tuple
- Applied to:
π See also π
make_tupleβ creates a tuple object of the type defined by the argument types (C++11) (function template)forward_as_tupleβ creates a tuple of forwarding references (C++11) (function template)applyβ calls a function with a tuple of arguments (C++17) (function template)
Source/version check: http://cppreference.com, dated 2024.06.10, manual std::make_from_tuple(3) β bye bestie, go make some tuples! π
π
π¦ 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