Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join two tuples in C++11

Tags:

c++

c++11

tuples

I've got two tuples, std::tuple<F1, F2, ..., FN>, std::tuple<G1, G2, ..., GN> (or std::tuple<G1> aka G1). Is there any way to join these tuples generically into a std::tuple<F1, F2, ..., FN, G1, G2, ..., GN> if any of the types F1, F2, ..., FN, G1, G2, ..., GN does not have a default constructor, but is movable / swapable?

like image 387
Markus Mayr Avatar asked Dec 17 '13 07:12

Markus Mayr


1 Answers

You can use std::tuple_cat

std::tuple<foo, bar, baz> buzz;
std::tuple<moo, meow, arf> bark;

auto my_cat_tuple = std::tuple_cat(buzz, std::move(bark));  // copy elements of buzz,
                                                            // move elements of bark

The above will work if the element types of the tuples are movable or copyable. And it does not require them to be default constructible unless you're doing something like

decltype(std::tuple_cat(buzz, bark)) my_uncatted_yet_tuple;  // This will attempt to default construct the tuple elements

my_uncatted_yet_tuple = std::tuple_cat(buzz, std::move(bark));
like image 161
Mark Garcia Avatar answered Oct 16 '22 06:10

Mark Garcia