wrap_function

#include <iostream>

template <typename T> class funct_wrap0
{
public:
    funct_wrap0(T (&f_)()):f(f_){}
    T operator()() const { return f(); }
private:
    T (&f)();
};
template <typename T, typename A0> class funct_wrap1
{
public:
    funct_wrap1(T (&f_)(A0)):f(f_){}
    T operator()(A0 a0) const { return f(a0); }
private:
    T (&f)(A0);
};
template <typename T, typename A0, typename A1> class funct_wrap2
{
public:
    funct_wrap2(T (&f_)(A0,A1)):f(f_){}
    T operator()(A0 a0, A1 a1) const { return f(a0,a1); }
private:
    T (&f)(A0,A1);
};
template <typename T, typename A0, typename A1, typename A2> class funct_wrap3
{
public:
    funct_wrap3(T (&f_)(A0,A1,A2)):f(f_){}
    T operator()(A0 a0, A1 a1, A2 a2) const { return f(a0,a1,a2); }
private:
    T (&f)(A0,A1,A2);
};
template <typename T, typename A0, typename A1, typename A2, typename A3> class funct_wrap4
{
public:
    funct_wrap4(T (&f_)(A0,A1,A2,A3)):f(f_){}
    T operator()(A0 a0, A1 a1, A2 a2, A3 a3) const { return f(a0,a1,a2,a3); }
private:
    T (&f)(A0,A1,A2,A3);
};
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4> class funct_wrap5
{
public:
    funct_wrap5(T (&f_)(A0,A1,A2,A3,A4)):f(f_){}
    T operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const { return f(a0,a1,a2,a3,a4); }
private:
    T (&f)(A0,A1,A2,A3,A4);
};


template <typename T>
void call_funct(const T& funct)
{  
    std::cout << funct(); 
}

std::string test() 
{
    return "asdfasf";
}

std::string print(std::string str){
    return str;
}


template <typename T>
funct_wrap0<T> wrap_function(T (&f)()) { return funct_wrap0<T>(f); }
template <typename T, typename A0>
funct_wrap1<T,A0> wrap_function(T (&f)(A0)) { return funct_wrap1<T,A0>(f); }
template <typename T, typename A0, typename A1>
funct_wrap2<T,A0,A1> wrap_function(T (&f)(A0, A1)) { return funct_wrap2<T,A0,A1>(f); }
template <typename T, typename A0, typename A1, typename A2>
funct_wrap3<T,A0,A1,A2> wrap_function(T (&f)(A0, A1, A2)) { return funct_wrap3<T,A0,A1,A2>(f); }
template <typename T, typename A0, typename A1, typename A2, typename A3>
funct_wrap4<T,A0,A1,A2,A3> wrap_function(T (&f)(A0, A1, A2, A3)) { return funct_wrap4<T,A0,A1,A2,A3>(f); }
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4>
funct_wrap5<T,A0,A1,A2,A3,A4> wrap_function(T (&f)(A0, A1, A2, A3, A4)) { return funct_wrap5<T,A0,A1,A2,A3,A4>(f); }


int main() {
    funct_wrap0<std::string> fwrap0(test);
    std::cout << fwrap0() << std::endl;
    call_funct(test);

    funct_wrap1<std::string, std::string> fwrap1(print);
    std::cout << fwrap1("test...") << std::endl;

    wrap_function(test);
    wrap_function(print)("test...");
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值