如何在可变参数模板中冒泡最后一个元素?

Hai*_*ait 2 c++ c++14

我有模板函数可以将可变参数模板作为(例如)作为 (int, int, double)

template<class... Arg>
void
bubble(const Arg &...arg)
{ another_function(arg...); }
Run Code Online (Sandbox Code Playgroud)

在函数内部,我必须使用不同的参数顺序调用(double, int, int)。我该如何实施?

Jar*_*d42 5

使用std::index_sequence,您可以执行以下操作:

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...>{});
}
Run Code Online (Sandbox Code Playgroud)