Markus Mayr
Markus Mayr

Reputation: 4118

Join two tuples in C++11

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?

Upvotes: 14

Views: 4308

Answers (1)

Mark Garcia
Mark Garcia

Reputation: 17708

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));

Upvotes: 24

Related Questions