36. function实现原理
Posted 干锅土鸡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了36. function实现原理相关的知识,希望对你有一定的参考价值。
void hello(string str)
cout << str << endl;
int sum(int a,int b)
return a+b;
template<typename FTy>
class myfunction
;
template<typename R,typename A1>
class myfunction<R(A1)>
public:
using PFUNC = R(*)(A1);
myfunction(PFUNC pfunc):_pfunc(pfunc)
R operator()(A1 arg)
return _pfunc(arg);
private:
PFUNC _pfunc;
;
template<typename R,typename A1,typename A2>
class myfunction<R(A1,A2)>
public:
using PFUNC = R(*)(A1,A2);
myfunction(PFUNC pfunc):_pfunc(pfunc)
R operator()(A1 arg1,A2 arg2)
return _pfunc(arg1,arg2);
private:
PFUNC _pfunc;
;
//以下为最终版本:
template<typename R,typename...A>
class myfunction<R(A...)>
public:
using PFUNC = R(*)(A...);
myfunction(PFUNC pfunc):_pfunc(pfunc)
R operator()(A... arg)
return _pfunc(arg...);
private:
PFUNC _pfunc;
;
int main()
myfunction<void(string)> func1(hello);
func1("hello world!");//func1.operator()("hello world!")
myfuncion<int(int,int)> func2(sum);
cout << func2(10,20) << endl;
以上是关于36. function实现原理的主要内容,如果未能解决你的问题,请参考以下文章
window.onload和JQuery中$(function(){})的区别即其实现原理
替换(或重新实现?)std::function 进行一些统计和测试