C++可变参数模板

参考:
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) 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值