InsideLoop
InsideLoop

Reputation: 6255

std::forward_as_tuple to pass arguments to 2 constructors

I would like to pass multiple arguments in order to construct two objects inside a function, the same way std::pair<T1, T2>(std::piecewise_construct, ...) works.

So I wrote

template <typename Args0..., typename Args1...>
void f(std::tuple<Arg0> args0, std::tuple<Args1> args1) {
   Object0 alpha(...);
   Object1 beta(...);
   ...
}

so I can call

f(std::forward_as_tuple(..., ..., ...), std::forward_as_tuple(..., ...))

But I don't know how to construct Object0 and Object1. I have checked the source code of my standard library for std::pair and they seems to use complicated internal functions to get the indices of args0 and args1. Do you have any idea on how to do that?

Upvotes: 5

Views: 950

Answers (1)

TartanLlama
TartanLlama

Reputation: 65620

C++17 will have make_from_tuple for doing this, but you can write this in C++11. Here's a C++14 version stolen from cppreference (for C++11 you can use the implementation of std::index_sequence from Implementation C++14 make_integer_sequence).

namespace detail {
template <class T, class Tuple, std::size_t... I>
constexpr T make_from_tuple_impl( Tuple&& t, std::index_sequence<I...> )
{
  return T(std::get<I>(std::forward<Tuple>(t))...);
}
} // namespace detail

template <class T, class Tuple>
constexpr T make_from_tuple( Tuple&& t )
{
    return detail::make_from_tuple_impl<T>(std::forward<Tuple>(t),
        std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}

With this utility, the implementation of f is a breeze:

template <typename... Args0, typename... Args1>
void f(std::tuple<Args0...> args0, std::tuple<Args1...> args1) {
   auto alpha = make_from_tuple<Object0>(args0);
   auto beta = make_from_tuple<Object1>(args1);
}

To make it more generic, I'd recommend just deducing the types of those tuples and perfect-forwarding them:

template <typename T0, typename T1>
void f(T0&& args0, T1&& args1) {
   auto alpha = make_from_tuple<Object0>(std::forward<T0>(args0));
   auto beta = make_from_tuple<Object1>(std::forward<T1>(args1));
}

Live C++11 demo

Upvotes: 4

Related Questions