绑定有n个参数的函数
#include <iostream>
#include <functional>
template<typename F, typename... Args>
void mybind(F&& f, Args&&... args)
{
//绑定必须先实例化函数
auto task = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // 这里使用了完美转发
task();
}
template<typename... Args>
void Print(const Args&... args) {
int tmp[] = { (std::cout << args << std::endl, 0)... };//打印传入参数
}
int main() {
//Print是函数,后面三个是参数
mybind(Print<int, int, int>, 1, 2, 3);
return 0;
}
根据我的尝试,想要把可以传入多个参数的函数绑定成函数需要先实例化函数,告诉Print函数我的函数传入了哪个类型,几个参数。才能让bind把Print函数绑定为task函数。不知道能否使用模板不实例化函数来绑定可以传入多个参数的函数。
萌新自己摸索的,有什么不对希望大佬指证。

浙公网安备 33010602011771号