3

I have template function that get variadic templates as (for example) as (int, int, double)

template<class... Arg>
void
bubble(const Arg &...arg)
{ another_function(arg...); }

Inside the function, I must call with a different order of parameters (double, int, int). How can I implement this?

thor
  • 21,418
  • 31
  • 87
  • 173
Hait
  • 115
  • 8
  • 2
    https://stackoverflow.com/questions/20162903/template-parameter-packs-access-nth-type-and-nth-element might help – An0num0us Nov 23 '17 at 16:42

1 Answers1

4

With std::index_sequence, you may do something like:

template <typename Tuple, std::size_t ... Is>
decltype(auto) bubble_impl(const Tuple& tuple, std::index_sequence<Is...>)
{
    constexpr auto size = sizeof...(Is);
    return another_function(std::get<(Is + size - 1) % size>(tuple)...);
}


template <class... Args>
decltype(auto) bubble(const Args &...args)
{
    return bubble_impl(std::tie(args...), std::index_sequence_for<Args...>{});
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302