Difference between revisions of "cpp/utility/tuple/make tuple"
From cppreference.com
(code not conform to the text "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&.") |
(→Possible implementation: tweak) |
||
Line 30: | Line 30: | ||
{{eq fun | 1= | {{eq fun | 1= | ||
template <class T> | template <class T> | ||
− | struct | + | struct unwrap_refwrapper |
{ | { | ||
using type = T; | using type = T; | ||
Line 36: | Line 36: | ||
template <class T> | template <class T> | ||
− | struct | + | struct unwrap_refwrapper<std::reference_wrapper<T>> |
{ | { | ||
using type = T&; | using type = T&; | ||
Line 42: | Line 42: | ||
template <class T> | template <class T> | ||
− | using special_decay_t = typename | + | using special_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type; |
template <class... Types> | template <class... Types> | ||
auto make_tuple(Types&&... args) | auto make_tuple(Types&&... args) | ||
{ | { | ||
− | return std::tuple<special_decay_t | + | return std::tuple<special_decay_t<Types>...>(std::forward<Types>(args)...); |
} | } | ||
}} | }} |
Revision as of 16:49, 20 February 2016
Defined in header <tuple>
|
||
template< class... Types > tuple<VTypes...> make_tuple( Types&&... args ); |
(since C++11) (until C++14) |
|
template< class... Types > constexpr tuple<VTypes...> make_tuple( Types&&... args ); |
(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&
.
Contents |
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)...).
Possible implementation
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 special_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type; template <class... Types> auto make_tuple(Types&&... args) { return std::tuple<special_decay_t<Types>...>(std::forward<Types>(args)...); } |
Example
Run this code
#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 t1 is (10, Test, 3.14, 7, 1) 5 7
(C++11) |
creates a tuple of lvalue references or unpacks a tuple into individual objects (function template) |
(C++11) |
creates a tuple of forwarding references (function template) |
(C++11) |
creates a tuple by concatenating any number of tuples (function template) |