How can I bubble last element in the variadic templates?

Viewed 112

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?

1 Answers

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...>{});
}
Related