我如何在变异模板中冒泡最后一个元素

How can I bubble last element in the variadic templates?

本文关键字:最后一个 元素 变异      更新时间:2023-10-16

i具有模板函数,可以将变量模板作为(例如)作为 (int, int, double)

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

在该功能中,我必须使用不同的参数 (double, int, int)调用。我该如何实施?

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