|
|
Member functions | ||||
Non-member functions | ||||
make_tuple | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
Helper concepts | ||||
(C++23) | ||||
Helper classes | ||||
(C++23) | ||||
(C++23) | ||||
Deduction guides(C++17) |
Defined in header <tuple> | ||
template<class...Types> std::tuple<VTypes...> make_tuple( Types&&...args); | (since C++11) (constexpr since C++14) | |
Creates a tuple object, deducing the target type from the types of arguments.
For eachTi
inTypes...
, the corresponding typeVi
inVTypes...
isstd::decay<Ti>::type unless application ofstd::decay results instd::reference_wrapper<X> for some typeX
, in which case the deduced type isX&
.
Contents |
args | - | zero or more arguments to construct the tuple from |
Astd::tuple object containing the given values, created as if bystd::tuple<VTypes...>(std::forward<Types>(t)...).
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<typenamestd::decay<T>::type>::type;// or use std::unwrap_ref_decay_t (since C++20) template<class...Types>constexpr// since C++14std::tuple<unwrap_decay_t<Types>...> make_tuple(Types&&...args){returnstd::tuple<unwrap_decay_t<Types>...>(std::forward<Types>(args)...);} |
#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 constructionint 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 valuesint 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
(C++11) | creates atuple of lvalue references or unpacks a tuple into individual objects (function template)[edit] |
(C++11) | creates atuple offorwarding references(function template)[edit] |
(C++11) | creates atuple by concatenating any number of tuples(function template)[edit] |
(C++17) | calls a function with a tuple of arguments (function template)[edit] |