简体   繁体   English

C ++使用元组作为模板中的参数包

[英]c++ use tuple as parameter pack in template

I am using boost::callable_traits::args_t to get std::tuple of arguments types from method. 我正在使用boost::callable_traits::args_t从方法中获取参数类型的std :: tuple。 Is it possible to use that tuple as parameter pack? 可以使用该元组作为参数包吗?

void doJob(int i) {
  std::cout << i << std::endl;
}
template<auto F>
void magic(boost::callable_traits::args_t<F> ...args) { // here is the magic
  F(args...)
}
magic<doJob>(1);

This is something I want to implement. 这是我要实现的东西。 I want to make this without using variadic templates. 我想在不使用可变参数模板的情况下做到这一点。

If you can use C++17 you can use std::apply to call the function. 如果可以使用C ++ 17,则可以使用std::apply调用该函数。 std::apply takes a callable and a tuple and calls the callable with the unpacked tuple as the function arguments. std::apply接受一个callable和一个tuple,并以未打包的tuple作为函数参数调用callable。 That would make magic look like 那会让magic看起来像

template<auto F>
void magic(boost::callable_traits::args_t<F> args) { // here is the magic
  std::apply(F, args);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM