Namespaces
Variants
Views
Actions

std::tuple_cat

From cppreference.com
< cpp‎ | utility‎ | tuple
Revision as of 10:54, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <tuple>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class... Tuples >
tuple<CTypes...> tuple_cat(Tuples&&... args);
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Constructs a tuple that is a concatenation of all tuples in args.

Parameters

args - zero or more tuples to concatenate

Return value

A Template:cpp/ltt object composed of all elements of all argument tuples constructed from std::get<i>(std::forward<Ti>(arg)) for each individual element.

Example

#include <iostream>
#include <tuple>
#include <string>
 
// 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<class... Args>
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_pair("Foo", "bar"), t1, std::tie(n));
    n = 10;
    print(t2);
}

Output:

(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 10)
creates a tuple object of the type defined by the argument types
(function template) [edit]
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template) [edit]
creates a tuple of rvalue references
(function template) [edit]