boost::bind实现原理学习
来源:互联网 发布:ubuntu解压tar文件 编辑:程序博客网 时间:2024/05/21 19:33
公司最近商业模式转变,很多项目都被暂停了,我们项目也是在暂停之列,而且部门同事也被裁掉一半。
在这个时间点,让我也有时间在工作时间了学习一把。对boost::bind学习的时候,顺便看到一边关于bind实现原理的讲解,在里面看到了在类中使用模板方式定义一个回调函数,于是想验证一把,但发现模板回调函数居然不能是全局的。可以在类里面定义,可以在函数参数里面定义。具体原因不是清楚。下面是对该bind函数实现原理的代码。原博客对bind的实现机制描述得非常透彻,忘了博客地址,不然是应该要贴出来的。在里面还涉及到我以前不知道int-To-Type的知识,主要是我对模板这块还不熟悉。下面看看代码吧
//testtemplatecallback.h#include <iostream>#include <string>using namespace std;template<typename X, typename Y>class TestCallBack{typedef X (*P_FN)(Y);public:TestCallBack(P_FN pfn):m_pFn(pfn){}void operator()(){m_pFn(2);}private:P_FN m_pFn;};/*//可以在类中使用回调函数模板,而不能定义全局回调函数模板//错误如下://error C2823: typedef 模板 非法//error C2998: “P (__cdecl *__cdecl P_FNZ)(Q)”: 不能是模板定义template<typename P,typename Q>typedef P (*P_FNZ)(Q);*///这个能够行template<typename M, typename N>int FuncCallBack(M (*P_pFN)(N)){return P_pFN(1);}int HelloCallBack(int ){cout<<"hello world"<<endl;return 0;}void TestCall_Back(){TestCallBack<int,int> TCB(HelloCallBack);TCB();FuncCallBack(HelloCallBack);}
下面就是bind库结合STL中的find_if的使用例子了,说实话对于C++的模板实现的东西,让我们的代码变简洁了,但同时也增加了代码的理解复杂度。
#include <string>#include <vector>#include <algorithm>#include <boost/bind.hpp>using namespace std;using namespace boost;class Person{public:Person(const string& name): name_(name){}string Name(){return name_;}string name_;};void TestBind(){typedef vector<Person> PersonList;PersonList personList;personList.push_back(Person("Ralph"));personList.push_back(Person("Joy"));personList.push_back(Person("Martin"));PersonList::iterator iter = find_if(personList.begin(), personList.end(),bind(&Person::Name, _1) == "Ralph");cout << (iter == personList.end() ? "Not found." : iter->Name().append(" found."))<< endl;}
- boost::bind实现原理学习
- boost:bind原理
- 【boost学习】之boost::bind
- boost bind 实现方法
- boost bind 实现方法
- boost bind 实现方法
- boost bind 实现方法
- Boost学习笔记-bind
- boost::bind 学习
- boost学习之bind
- boost 线程学习bind
- Boost::bind学习
- boost::bind和占位符实现的原理(from AV BOOST)
- Boost学习笔记之bind
- boost::bind函数原理和使用
- boost::function 和 boost::bind 的工作原理
- C++学习 boost学习之-bind
- 详细解析boost中bind的实现
- java.util.ConcurrentModificationException
- android 图形系统加速学习系列 (二)
- unicode cstring to char*
- HWND GetWindowHandleByPID(DWORD dwProcessID)
- Facebook scribe部署实践
- boost::bind实现原理学习
- SharedPreferences Android
- 跟着门户学css 3 qq.com全局导航栏和SOSO搜索栏
- 命运总是这样安排
- GXT(GWT-EXT)开发坏境搭建
- 在对话框上拖动按钮并移动该按钮
- VC限制只能输入常规数字的CEdit控件
- 英语单词积累
- 上海经济适用房申请条件