Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a tuple with four elements from two pairs with matching data types in C++?

I have a function f(), that returns a std::pair<A, B> with some types A and B. And I have another function g(), that calls f() twice and returns a std::tuple<A, B, A, B>. Is there a way to construct the return tuple directly from both calls to f()? So I would like to shortcut the last three lines of my current code:

std::tuple<A, B, A, B>
g(type1 arg1, type2 arg2, ...) {
    // perform some preprocessing
    auto pair1 = f(...);
    auto pair2 = f(...);
    return { pair1.first, pair1.second, pair2.first, pair2.second }
}

into a one liner (instead of three). It would be nice, if that solution also worked with tuples of arbitrary length, especially in a template situation.

For example, in current Python3, the solution would be return (*f(...), *f(...)). Is there a similiar list/tuple unwrap operator in C++ as there is in Python?

like image 617
Kai Petzke Avatar asked Aug 17 '21 17:08

Kai Petzke


People also ask

What is difference between tuple and pair?

std::tuple is not required by the standard to ever be standard-layout. Every std::pair<T, Y> is standard-layout if both T and Y are standard-layout. It's a bit easier to get the contents of a pair than a tuple . You have to use a function call in the tuple case, while the pair case is just a member field.

What is std :: tuple?

(since C++11) Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair. If std::is_trivially_destructible<Ti>::value is true for every Ti in Types , the destructor of tuple is trivial.


1 Answers

Yes, there is std::tuple_cat. Even though cppreference says that...

The behavior is undefined if any type in [arguments] 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.

I've tested it and all major standard library implementations (libstdc++, libc++, and MSVC's library) do support it.

Example:

#include <tuple>

struct A {};
struct B {};

std::pair<A, B> foo() {return {};}

std::tuple<A, B, A, B> bar()
{
    return std::tuple_cat(foo(), foo());
}
like image 70
HolyBlackCat Avatar answered Oct 16 '22 11:10

HolyBlackCat