Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | tuple
(Created page with "{{cpp/title|make_tuple}} {{cpp/utility/tuple/sidebar}} {{ddcl list begin}} {{ddcl list header | utility}} {{ddcl list item | 1= template< class... Types > tuple<VTypes...> make_t...")
 
m (Example: fmt)
 
(35 intermediate revisions by 16 users not shown)
Line 1: Line 1:
 
{{cpp/title|make_tuple}}
 
{{cpp/title|make_tuple}}
{{cpp/utility/tuple/sidebar}}
+
{{cpp/utility/tuple/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | utility}}
+
{{dcl header|tuple}}
{{ddcl list item | 1=
+
{{dcl|since=c++11|notes={{mark|constexpr since C++14}}|1=
 
template< class... Types >
 
template< class... Types >
tuple<VTypes...> make_tuple(Types&&... args);
+
std::tuple<VTypes...> make_tuple( Types&&... args );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Creates a tuple object, deducing the target type from the types of arguments. The deduced types are {{cpp|std::decay<Ti>::type}} (transformed as if passed to a function by value) unless application of {{cpp|std::decay}} results in {{cpp|std::reference_wrapper<X>}} for some type {{tt|X}}, in which case the deduced type is is {{tt|X&}}.
+
Creates a tuple object, deducing the target type from the types of arguments.
 +
 
 +
For each {{tt|Ti}} in {{tt|Types...}}, the corresponding type {{tt|Vi}} in {{tt|VTypes...}} is {{c|std::decay<Ti>::type}} unless application of {{lc|std::decay}} results in {{c|std::reference_wrapper<X>}} for some type {{tt|X}}, in which case the deduced type is {{tt|X&}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | args | zero or more arguments to construct the tuple from}}
+
{{par|args|zero or more arguments to construct the tuple from}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
A {{cpp/ltt|cpp/utility/tuple}} object containing the given values.
+
A {{lc|std::tuple}} object containing the given values, created as if by {{c|std::tuple<VTypes...>(std::forward<Types>(t)...).}}
 +
 
 +
===Possible implementation===
 +
{{eq fun|1=
 +
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<typename std::decay<T>::type>::type;
 +
// or use std::unwrap_ref_decay_t (since C++20)
 +
 
 +
template <class... Types>
 +
constexpr // since C++14
 +
std::tuple<unwrap_decay_t<Types>...> make_tuple(Types&&... args)
 +
{
 +
    return std::tuple<unwrap_decay_t<Types>...>(std::forward<Types>(args)...);
 +
}
 +
}}
  
 
===Example===
 
===Example===
{{example cpp |
+
{{example|
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <tuple>
 
#include <tuple>
 
#include <functional>
 
#include <functional>
int main()
+
 
 +
std::tuple<int, int> f() // this function returns multiple values
 
{
 
{
     auto t1 = std::make_tuple(10, "Test", 3.14);
+
     int x = 5;
     std::cout << "The value of t1 is "
+
     return std::make_tuple(x, 7); // return {x,7}; in C++17
              << "(" << std::get<0>(t1) << ", " << std::get<1>(t1)
+
}
              << ", " << std::get<2>(t1) << ")\n";
+
  
 +
int main()
 +
{
 +
    // heterogeneous tuple construction
 
     int n = 1;
 
     int n = 1;
     auto t2 = std::make_tuple(std::ref(n), n);
+
     auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
 
     n = 7;
 
     n = 7;
     std::cout << "The value of t2 is "
+
     std::cout << "The value of t is ("
               << "(" << std::get<0>(t2) << ", " << std::get<1>(t2) << ")\n";
+
               << 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=
+
|output=
The value of t1 is (10, Test, 3.14)
+
The value of t is (10, Test, 3.14, 7, 1)
The value of t2 is (7, 1)
+
5 7
 
}}
 
}}
  
{{dcl list begin}}
+
===See also===
{{dcl list tfun | cpp/utility/tuple/tie | creates a {{tt|tuple}} of lvalue references or unpacks a tuple into individual objects }}
+
{{dsc begin}}
{{dcl list tfun | cpp/utility/tuple/forward_as_tuple | creates a {{tt|tuple}} of rvalue references }}
+
{{dsc inc|cpp/utility/tuple/dsc tie}}
{{dcl list end}}
+
{{dsc inc|cpp/utility/tuple/dsc forward_as_tuple}}
 +
{{dsc inc|cpp/utility/tuple/dsc tuple_cat}}
 +
{{dsc inc|cpp/utility/dsc apply}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 08:46, 2 February 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
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 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

[edit] Parameters

args - zero or more arguments to construct the tuple from

[edit] Return value

A std::tuple object containing the given values, created as if by std::tuple<VTypes...>(std::forward<Types>(t)...).

[edit] 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 unwrap_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type;
// or use std::unwrap_ref_decay_t (since C++20)
 
template <class... Types>
constexpr // since C++14
std::tuple<unwrap_decay_t<Types>...> make_tuple(Types&&... args)
{
    return std::tuple<unwrap_decay_t<Types>...>(std::forward<Types>(args)...);
}

[edit] Example

#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 t is (10, Test, 3.14, 7, 1)
5 7

[edit] See also

(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]
(C++17)
calls a function with a tuple of arguments
(function template) [edit]