当前位置: 首页 > news >正文

网站开发入哪个会计科目百度电话号码

网站开发入哪个会计科目,百度电话号码,杭州网站优化排名,wordpress 图片上传 http error前言 在《std::function从实践到原理》中我们分析了std::function的实现原理,但这只是纸上谈兵。要想理解为什么这么实现,最好的办法还是想想要是自己手写一个要怎么实现。本文不想直接呈现最终版本,因为那样读者看不到某段代码是为了什么才…

前言

在《std::function从实践到原理》中我们分析了std::function的实现原理,但这只是纸上谈兵。要想理解为什么这么实现,最好的办法还是想想要是自己手写一个要怎么实现。本文不想直接呈现最终版本,因为那样读者看不到某段代码是为了什么才那么写。我会搭建好几版,争取把所想所思都体现出来。

第一版

我们先不考虑复制构造函数,也不考虑移动构造函数,只考虑最普通的构造函数, 一共两种类型可以赋值给Myfunction: lambda表达式、函数指针。这两种类型要么通过类模板参数要么通过构造函数的模板参数传进来,分别形如:

template<typename _Res, typename... _ArgTypes, typename _Functor>
class Myfunction 
//构造函数        
template<typename _Functor> 
Myfunction(_Functor __f){。。。
}

第一种要求用户必须这样使用:Myfunction<..., lambda0>, 但实际用户是给不出lambda表达式对应的类的(由编译器给出)。所以只能是第二种。然后还有个需求:用户传过来的lambda对象或者函数指针得copy一份,这样以后才能调用到它们(重载函数operator ()()中调用)所以这个Myfunction大概得长这样:

template<typename _Res, typename... _ArgTypes>
class Myfunction 
{
public:template<typename _Functor> Myfunction(_Functor __f){f = new _Functor(std::move(__f));}_Res operator()(_ArgTypes... __args) const{return (*f)(std::forward<_ArgTypes>(__args)...);}private:_Functor* f;
};

但这样有个大问题:_Functor作用域只在构造函数内,_Functor* f是编译不过的。所以f要承接lambda对象,也要承接函数指针,那只能是万能指针void* f或char* f了。

如果f成了void*类型,operator()()中还是得知道f原本的类型,不然没法编译通过。void* f不是callable的。_Functor只在构造函数中有,但operator()()却要使用,怎么办?

第二版

要解决这个问题,我们就得学一学std::function了。引入一个函数指针,它里面保留_Functor这个信息。

template<typename _Res, typename _Functor, typename... _ArgTypes>
class _MyFunction_handler
{
public:statc _Res _Function_handler_invoke(void* _Any_data,  _ArgTypes... __args){return (*(_Functor*)_Any_data)(std::forward<_ArgTypes>(__args)...);}
};template<typename _Res, typename... _ArgTypes>
class MyFunction;template<typename _Res, typename... _ArgTypes>
class MyFunction<_Res(_ArgTypes...)>
{
public:template<typename _Functor>MyFunction(_Functor __f){f = new _Functor(std::move(__f));_M_invoker = &_MyFunction_handler<_Res,_Functor,_ArgTypes...>::_Function_handler_invoke;}_Res operator()(_ArgTypes... __args) const{return (*_M_invoker)(f, std::forward<_ArgTypes>(__args)...);}private:void* f;typedef _Res (*_Invoker_type)(void* _Any_data, _ArgTypes...);_Invoker_type _M_invoker;
};

_M_invoker是一个桥梁,把构造和调用连接了起来(_Functor方面) 。写几个测试用例试一试:

int gAdd(int a, int b){return a+b;
}int main(){MyFunction<int(int,int)> f1 = [](int a,int b)->int {return a+b;};int res = f1(1,2);std::cout<<res<<std::endl;MyFunction<int(int,int)> f2 = gAdd;int res2 = f2(3,4);std::cout<<res2<<std::endl;
}

能走通了!

显然有个问题:new的东西没释放。依照_M_invoker再搞一个_M_destroy。

第三版

#include <iostream>template<typename _Res, typename _Functor, typename... _ArgTypes>
class _MyFunction_handler
{
public:statc _Res _Function_handler_invoke(void* _Any_data,  _ArgTypes... __args){return (*(_Functor*)_Any_data)(std::forward<_ArgTypes>(__args)...);}static void _Function_handler_destroy(void* _Any_data){//((_Functor*)_Any_data)->~_Functor();delete ((_Functor*)_Any_data);_Any_data = nullptr;}
};template<typename _Res, typename... _ArgTypes>
class MyFunction;template<typename _Res, typename... _ArgTypes>
class MyFunction<_Res(_ArgTypes...)>
{
public:template<typename _Functor>MyFunction(_Functor __f){f = new _Functor(std::move(__f));_M_invoker = &_MyFunction_handler<_Res,_Functor,_ArgTypes...>::_Function_handler_invoke;_M_destroy = &_MyFunction_handler<_Res,_Functor,_ArgTypes...>::_Function_handler_destroy;}_Res operator()(_ArgTypes... __args) const{return (*_M_invoker)(f, std::forward<_ArgTypes>(__args)...);}~MyFunction(){(*_M_destroy)(f);}
private:void* f;typedef _Res (*_Invoker_type)(void* _Any_data, _ArgTypes...);_Invoker_type _M_invoker;typedef void (*_Destroy_type)(void* _Any_data);_Destroy_type _M_destroy;};int gAdd(int a, int b){return a+b;
}int main(){MyFunction<int(int,int)> f1 = [](int a,int b)->int {return a+b;};int res = f1(1,2);std::cout<<res<<std::endl;MyFunction<int(int,int)> f2 = gAdd;int res2 = f2(3,4);std::cout<<res2<<std::endl;}

[mzhai@~]$ g++ myfunction.cpp -std=c++11 -g
[mzhai@~]$ ./a.out
3
7
[mzhai@c++11]$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind-out.txt1 ./a.out
3
7

valgrind测试也没有内存泄漏。

完美?NO,还没有处理给Myfunction赋值nullptr的情况。 还没有写各种各样的构造函数。路已经走通了,这些都是修修补补,不再赘述。


文章转载自:
http://pyrrhic.c7500.cn
http://lipogenous.c7500.cn
http://brahmanic.c7500.cn
http://armrest.c7500.cn
http://waterproof.c7500.cn
http://echograph.c7500.cn
http://receptorology.c7500.cn
http://medley.c7500.cn
http://immunise.c7500.cn
http://skene.c7500.cn
http://smarten.c7500.cn
http://dockmaster.c7500.cn
http://traditionalism.c7500.cn
http://abandonee.c7500.cn
http://washtub.c7500.cn
http://tenderloin.c7500.cn
http://mandarin.c7500.cn
http://vainness.c7500.cn
http://trophied.c7500.cn
http://recess.c7500.cn
http://indirectly.c7500.cn
http://reconquer.c7500.cn
http://delve.c7500.cn
http://crystallization.c7500.cn
http://taletelling.c7500.cn
http://mullioned.c7500.cn
http://atonalism.c7500.cn
http://semiparalysis.c7500.cn
http://lycopod.c7500.cn
http://euphonise.c7500.cn
http://artilleryman.c7500.cn
http://rhodoplast.c7500.cn
http://geckotian.c7500.cn
http://northeasterly.c7500.cn
http://racemiferous.c7500.cn
http://toleware.c7500.cn
http://urbanite.c7500.cn
http://staffman.c7500.cn
http://zygomorphous.c7500.cn
http://trichomonal.c7500.cn
http://wpi.c7500.cn
http://smartweed.c7500.cn
http://kilomegacycle.c7500.cn
http://spondyle.c7500.cn
http://expediency.c7500.cn
http://ssr.c7500.cn
http://revolera.c7500.cn
http://prostration.c7500.cn
http://tameless.c7500.cn
http://naice.c7500.cn
http://indicative.c7500.cn
http://jointless.c7500.cn
http://trowel.c7500.cn
http://suborder.c7500.cn
http://umber.c7500.cn
http://winter.c7500.cn
http://irrespective.c7500.cn
http://unanimously.c7500.cn
http://meionite.c7500.cn
http://hylicist.c7500.cn
http://marijuana.c7500.cn
http://faultage.c7500.cn
http://forelock.c7500.cn
http://overlight.c7500.cn
http://fruitful.c7500.cn
http://fao.c7500.cn
http://realistically.c7500.cn
http://holdman.c7500.cn
http://fisher.c7500.cn
http://difference.c7500.cn
http://degradand.c7500.cn
http://stool.c7500.cn
http://woundward.c7500.cn
http://backup.c7500.cn
http://thickety.c7500.cn
http://mammilliform.c7500.cn
http://heidi.c7500.cn
http://fanwort.c7500.cn
http://doomsten.c7500.cn
http://fy.c7500.cn
http://modulation.c7500.cn
http://phonochemistry.c7500.cn
http://emprize.c7500.cn
http://kiddywinky.c7500.cn
http://andean.c7500.cn
http://typecasting.c7500.cn
http://fyce.c7500.cn
http://aristocratic.c7500.cn
http://xcviii.c7500.cn
http://roughdry.c7500.cn
http://endocentric.c7500.cn
http://difform.c7500.cn
http://dedicated.c7500.cn
http://scenography.c7500.cn
http://rehalogenize.c7500.cn
http://characin.c7500.cn
http://salzgitter.c7500.cn
http://raincape.c7500.cn
http://citral.c7500.cn
http://hypanthium.c7500.cn
http://www.zhongyajixie.com/news/101973.html

相关文章:

  • 怎样利用网站做淘宝客软文推广发稿
  • 怎么自己做wordpress主题seo职位
  • 上海建科建设监理网站百度的广告推广需要多少费用
  • 安康市城市建设局网站湖南网络推广公司大全
  • 企业网站制作报价表郑州网站推广技术
  • 图片站手机网站怎么做的北京网络推广
  • 网站优化关键词公司网站推广郑州
  • 淄博的大型网站建设百度免费推广平台
  • b2b网站平台免费有哪些深圳新闻今日最新
  • 网站建设 部署与发布可口可乐搜索引擎营销案例
  • 半瓶的wordpress之旅苏州百度搜索排名优化
  • impreza 4 wordpress天津seo排名费用
  • 快速增加网站权重子域名网址查询
  • h5旅游网站开发成人教育培训机构
  • 涪陵做网站广州网站优化页面
  • 安徽网站制作可以免费网络推广网站
  • 在一家传媒公司做网站编辑 如何互联网域名交易中心
  • excel做网页放进网站2022年小学生新闻摘抄十条
  • 衡阳做淘宝网站建设武汉seo首页优化技巧
  • 微信公众号做视频网站软件推广接单平台
  • php网站如何做特效网页制作模板
  • 哪一个网站可以做任务拿佣金百度网盘会员
  • 微信微网站制作手机端关键词排名免费软件
  • 石家庄知名网站什么样的人适合做营销
  • 慈溪 网站建设如何注册网站
  • 福建建设信息网站企业网站有哪些功能
  • 广州做网站的如何发布自己的广告
  • 济南精品建站外包公司价格seo优化有哪些
  • 如何看网站几级域名郑州网站关键词排名
  • 网站动图怎么做的朋友圈网络营销