Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/tuple/make tuple"

From cppreference.com
< cpp‎ | utility‎ | tuple
(c++14/constexpr)
Line 6: Line 6:
 
{{dcl | since=c++11 | until=c++14 | 1=
 
{{dcl | since=c++11 | until=c++14 | 1=
 
template< class... Types >
 
template< class... Types >
tuple<VTypes...> make_tuple( Types&&... args );
+
{{lc|std::tuple}}<VTypes...> make_tuple( Types&&... args );
 
}}
 
}}
 
{{dcl | since=c++14 | 1=
 
{{dcl | since=c++14 | 1=
 
template< class... Types >
 
template< class... Types >
constexpr tuple<VTypes...> make_tuple( Types&&... args );
+
constexpr {{lc|std::tuple}}<VTypes...> make_tuple( Types&&... args );
 
}}
 
}}
 
{{dcl rev end}}
 
{{dcl rev end}}

Revision as of 15:26, 14 December 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <tuple>
template< class... Types >
<span class="t-lc">[[cpp/utility/tuple|std::tuple]]</span><VTypes...> make_tuple( Types&&... args );
(since C++11)
(until C++14)
template< class... Types >
constexpr <span class="t-lc">[[cpp/utility/tuple|std::tuple]]</span><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 special_decay
  {
     using type = typename std::decay<T>::type;
  };
 
template <class T>
 struct special_decay<std::reference_wrapper<T>>
 {
   using type = T&;
 };
 
template <class T>
 using special_decay_t = typename special_decay<T>::type;
 
template <class... Types>
 auto make_tuple(Types&&... args)
 {
   return std::tuple<special_decay_t<Types>...>(std::forward<Types>(args)...);
 }

Example

#include <iostream>
#include <tuple>
#include <functional>
 
int main()
{
    auto t1 = std::make_tuple(10, "Test", 3.14);
    std::cout << "The value of t1 is "
              << "(" << std::get<0>(t1) << ", " << std::get<1>(t1)
              << ", " << std::get<2>(t1) << ")\n";
 
    int n = 1;
    auto t2 = std::make_tuple(std::ref(n), n);
    n = 7;
    std::cout << "The value of t2 is "
              << "(" << std::get<0>(t2) << ", " << std::get<1>(t2) << ")\n";
}

Output:

The value of t1 is (10, Test, 3.14)
The value of t2 is (7, 1)
(C++11)
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template) [edit]
creates a tuple of forwarding references
(function template) [edit]
(C++11)
creates a tuple by concatenating any number of tuples
(function template) [edit]