vendredi 29 mai 2015

Unpack ts... to t0.a(), t0.b(), t1.a(), t1.b(),

I want to call one variadic function from another variadic function in the following manner:

template <typename ...Ts>
void f(Ts const & ...) { /* ... */ }

template <typename ...Args>
void g(Args const & ...args)
{
  // shall call f(arg0.a(), arg0.b(), arg1.a(), arg1.b(), ...)
}

I have done it the following way:

struct sentinel_t { };

template <typename Arg, typename ...Args>
void g_impl(Arg const & arg, Args const & ...args)
{
  g_impl(args..., arg.a(), arg.b());
}

template <typename ...Ts>
void g_impl(sentinel_t , Ts const & ...ts)
{
  f(ts...);
}

template <typename ...Args>
void g(Args const & ...args)
{
  g_impl(args..., sentinel_t{});
}

Is there another/better way to implement this pattern?

Aucun commentaire:

Enregistrer un commentaire