Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | tuple
m (Text replace - "{{example cpp" to "{{example")
(Added some description.)
 
(19 intermediate revisions by 11 users not shown)
Line 1: Line 1:
 
{{cpp/title|tuple_cat}}
 
{{cpp/title|tuple_cat}}
{{cpp/utility/tuple/sidebar}}
+
{{cpp/utility/tuple/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | tuple}}
+
{{dcl header|tuple}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{dcl rev multi|since1=c++11|until1=c++14|dcl1=
 
template< class... Tuples >
 
template< class... Tuples >
tuple<CTypes...> tuple_cat(Tuples&&... args);
+
std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
 +
|until2=c++23|dcl2=
 +
template< class... Tuples >
 +
constexpr std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
 +
|dcl3=
 +
template< tuple-like... Tuples >
 +
constexpr std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
 +
}}
 +
{{dcl end}}
 +
 
 +
Constructs a tuple that is a concatenation of all tuples in {{c|args}}. The element types {{c/core|/* CTypes */ }} of the returned tuple is formed by concatenating the elements type packs of all {{rev inl|until=c++23|{{lc|std::tuple}}}}{{rev inl|since=c++23|{{rlpi|tuple-like}}}} types in {{tt|Tuples}} in order.
 +
 
 +
{{rrev multi|until1=c++23
 +
|rev1=
 +
The behavior is undefined if any type in {{c/core|std::decay_t<Tuples>...}} is not a specialization of {{lc|std::tuple}}. However, an implementation may choose to support types (such as {{lc|std::array}} and {{lc|std::pair}}) that follow the tuple-like protocol.
 +
|rev2=
 +
The types {{c/core|std::decay_t<Tuples>...}} are constrained to be tuple-like, i.e. each type therein is required to be a specialization of {{lc|std::tuple}} or another type (such as {{lc|std::array}} and {{lc|std::pair}}) that models {{rlpi|tuple-like}}.
 
}}
 
}}
{{ddcl list end}}
 
  
Constructs a tuple that is a concatenation of all tuples in {{tt|args}}.
+
If any type in {{c/core|/* CTypes */}} is not constructible from the type of the corresponding element in the sequence of elements concatenated from {{c|args}}, {{rev inl|until=c++23|the behavior is undefined}}{{rev inl|since=c++23|the program is ill-formed}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | args | zero or more tuples to concatenate}}
+
{{par|args|zero or more tuples to concatenate}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
A {{cpp/ltt|cpp/utility/tuple}} object composed of all elements of all argument tuples constructed from {{cpp|std::get<i>(std::forward<Ti>(arg))}} for each individual element.
+
A {{lc|std::tuple}} object composed of all elements of all argument tuples constructed from {{c|std::get<j>(std::forward<Ti>(arg))}} for each individual element.
  
 
===Example===
 
===Example===
{{example |
+
{{example|
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
#include <tuple>
 
 
#include <string>
 
#include <string>
 +
#include <tuple>
  
 
// helper function to print a tuple of any size
 
// helper function to print a tuple of any size
 
template<class Tuple, std::size_t N>
 
template<class Tuple, std::size_t N>
struct TuplePrinter {
+
struct TuplePrinter
     static void print(const Tuple& t)  
+
{
 +
     static void print(const Tuple& t)
 
     {
 
     {
         TuplePrinter<Tuple, N-1>::print(t);
+
         TuplePrinter<Tuple, N - 1>::print(t);
 
         std::cout << ", " << std::get<N-1>(t);
 
         std::cout << ", " << std::get<N-1>(t);
 
     }
 
     }
Line 37: Line 53:
  
 
template<class Tuple>
 
template<class Tuple>
struct TuplePrinter<Tuple, 1> {
+
struct TuplePrinter<Tuple, 1>
     static void print(const Tuple& t)  
+
{
 +
     static void print(const Tuple& t)
 
     {
 
     {
 
         std::cout << std::get<0>(t);
 
         std::cout << std::get<0>(t);
Line 44: Line 61:
 
};
 
};
  
template<class... Args>
+
template<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0>
void print(const std::tuple<Args...>& t)  
+
void print(const std::tuple<Args...>& t)
 +
{
 +
    std::cout << "()\n";
 +
}
 +
 
 +
template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0>
 +
void print(const std::tuple<Args...>& t)
 
{
 
{
 
     std::cout << "(";
 
     std::cout << "(";
Line 57: Line 80:
 
     std::tuple<int, std::string, float> t1(10, "Test", 3.14);
 
     std::tuple<int, std::string, float> t1(10, "Test", 3.14);
 
     int n = 7;
 
     int n = 7;
     auto t2 = std::tuple_cat(t1, std::make_pair("Foo", "bar"), t1, std::tie(n));
+
     auto t2 = std::tuple_cat(t1, std::make_tuple("Foo", "bar"), t1, std::tie(n));
     n = 10;
+
     n = 42;
 
     print(t2);
 
     print(t2);
 
}
 
}
| output=
+
|output=
(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 10)
+
(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)
 
}}
 
}}
  
{{dcl list begin}}
+
===See also===
{{dcl list template | cpp/utility/tuple/dcl list make_tuple}}
+
{{dsc begin}}
{{dcl list template | cpp/utility/tuple/dcl list tie}}
+
{{dsc inc|cpp/utility/tuple/dsc make_tuple}}
{{dcl list template | cpp/utility/tuple/dcl list forward_as_tuple}}
+
{{dsc inc|cpp/utility/tuple/dsc tie}}
{{dcl list end}}
+
{{dsc inc|cpp/utility/tuple/dsc forward_as_tuple}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 01:44, 16 May 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <tuple>
template< class... Tuples >
std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
(since C++11)
(until C++14)
template< class... Tuples >
constexpr std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
(since C++14)
(until C++23)
template< tuple-like... Tuples >
constexpr std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
(since C++23)

Constructs a tuple that is a concatenation of all tuples in args. The element types /* CTypes */ of the returned tuple is formed by concatenating the elements type packs of all std::tuple(until C++23)tuple-like(since C++23) types in Tuples in order.

The behavior is undefined if any type in std::decay_t<Tuples>... is not a specialization of std::tuple. However, an implementation may choose to support types (such as std::array and std::pair) that follow the tuple-like protocol.

(until C++23)

The types std::decay_t<Tuples>... are constrained to be tuple-like, i.e. each type therein is required to be a specialization of std::tuple or another type (such as std::array and std::pair) that models tuple-like.

(since C++23)

If any type in /* CTypes */ is not constructible from the type of the corresponding element in the sequence of elements concatenated from args, the behavior is undefined(until C++23)the program is ill-formed(since C++23).

Contents

[edit] Parameters

args - zero or more tuples to concatenate

[edit] Return value

A std::tuple object composed of all elements of all argument tuples constructed from std::get<j>(std::forward<Ti>(arg)) for each individual element.

[edit] Example

#include <iostream>
#include <string>
#include <tuple>
 
// helper function to print a tuple of any size
template<class Tuple, std::size_t N>
struct TuplePrinter
{
    static void print(const Tuple& t)
    {
        TuplePrinter<Tuple, N - 1>::print(t);
        std::cout << ", " << std::get<N-1>(t);
    }
};
 
template<class Tuple>
struct TuplePrinter<Tuple, 1>
{
    static void print(const Tuple& t)
    {
        std::cout << std::get<0>(t);
    }
};
 
template<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
    std::cout << "()\n";
}
 
template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")\n";
}
// end helper function
 
int main()
{
    std::tuple<int, std::string, float> t1(10, "Test", 3.14);
    int n = 7;
    auto t2 = std::tuple_cat(t1, std::make_tuple("Foo", "bar"), t1, std::tie(n));
    n = 42;
    print(t2);
}

Output:

(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)

[edit] See also

creates a tuple object of the type defined by the argument types
(function template) [edit]
(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]