参考:
https://www.jianshu.com/p/4cddddfab980
https://blog.csdn.net/zhang_xiaoqiang/article/details/120737414
实例1
#include <iostream>
#include <functional>
#include <stdio.h>
int funref0(int i, int j) {
printf("funcref0: %d %d \n", i , j);
return 0;
}
class Func {
public:
int fun0(int i, int j) {
printf("func0: %d %d ", i , j);
return 0;
}
};
class Proxy {
public:
template<class F, class... Args> int sched(F&& f, Args&&... args);
};
template<class F, class... Args> int Proxy::sched(F&& f, Args&&... args) {
f(args...);
return 0;
}
int main(int argc, char **argv)
{
printf("test\n");
Proxy proxy;
proxy.sched(&funref0, 1, 2);
return 0;
}
实现2:
#include <iostream>
#include <functional>
#include <stdio.h>
int funref0(int i, int j) {
printf("funcref0: %d %d \n", i , j);
return 0;
}
class Func {
public:
int fun0(int i, int j) {
printf("func0: %d %d ", i , j);
return 0;
}
};
class Proxy {
public:
template<class Obj, class Function, class... Args> int sched(Obj&& obj, Function&& f, Args&&... args);
};
template<class Obj, class Function, class... Args> int Proxy::sched(Obj&& obj, Function&& f, Args&&... args) {
//
printf("3 param\n");
(obj->*f)(args...);
return 0;
}
int main(int argc, char **argv)
{
printf("test\n");
Proxy proxy;
Func fun;
proxy.sched(&fun, &Func::fun0, 5, 6);
return 0;
}
总结:
实例1 和 2 方法有冲突。
template<class Obj, class Function, class… Args> int sched(Obj&& obj, Function&& f, Args&&… args);
会覆盖
template<class Function, class… Args> int Proxy::sched(Function&& f, Args&&… args) 。