如何切片可变参数模板参数并使用它们?

How can I slice variadic template parameters and use them?

本文关键字:参数 切片 变参 何切片      更新时间:2023-10-16

我对模板元编程很陌生。这就是我想做的。

template <typename... Args>
void to_be_async_func(Args&&... args)
{
//want to use args[0...N-2] to invoke a function call
//and args[N-1(last)] to use a handler function after the prior call.
}
int main()
{
int a = 5;
int b = 3;
to_be_async_func(a, b, [](int res)
{
//do something with the result
//any Callable can be passed
});
}

最初我尝试像

template<typename... Args, typename Callable>
void to_be_async_func(Args&&... args, Callable op)
{
}

但是在这种情况下,"可调用"应该具有默认值才能像这样使用。
在我看来,某种辅助模板结构可能会做到这一点。
如果这件事有可能,你能给我指路吗?
提前谢谢你。

编辑我也
看过这篇文章并按照指示尝试。
成功了。但正如文中所述,我想知道是否有更标准的方法来实现这一点。

使用std::index_sequence似乎是要走的路:

template <typename F, typename... Args>
void to_be_async_func_first(F&& f, Args&&... args)
{
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}
template <std::size_t ... Is, typename... Args>
void to_be_async_func_impl(std::index_sequence<Is...>, Args&&... args)
{
auto&& args_tuple = std::forward_as_tuple(std::forward<Args>(args)...);
to_be_async_func_first(std::get<sizeof...(Args) - 1>(args_tuple),
std::get<Is>(args_tuple)...);
}
template <typename... Args>
void to_be_async_func(Args&&... args)
{
to_be_async_func_impl(std::make_index_sequence<sizeof...(Args) - 1>(),
std::forward<Args>(args)...);
}