template< class T, class Tuple >
constexpr T make_from_tuple( Tuple&& t );
template< class T, tuple-like Tuple >
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.
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
the program is ill-formed.
[edit] Parameters t - tuple whose elements to be used as arguments to the constructor ofT
[edit] Return value
The constructed T
object or reference.
Due to guaranteed copy elision, T
need not be movable.
#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:
[edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3528 C++17 cast containing reinterpret_cast etc. was allowed in the case of 1-tuple prohibited [edit] See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4