C++11中的std::bind
C++11中的std::bind
最近在看看cocos2dx的源代码,发现了cocos2dx 3.0相对于2.0改动了很多,最大的改变就是大量的使用了C++11的特性,比如auto
等。其中有一个关于回调函数绑定的宏定义就使用了std::bind
特性
// new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
非常简练的宏定义,对于没有接触过C++11的朋友来说,真的是一头雾水,这货是什么,真的是C++吗?所以笔者就写了这篇文章来讲解。
C++发展的历史
C++经过多年发展,真正正式公布出的标准只有三个C++98,C++03,C++11。其中C++03只是C++98的小幅度修订,在笔者看来,C++发展的历史,就是一个不断吸收新特性的历史。从最早的面向过程,面向对象,模板编程,到现在的函数式编程,C++一直都是在吸收新特性。lambda
就是函数式编程闭包的特性。
何为bind
bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调 用实体,这种机制在回调函数的使用过程中也颇为有用。其实最早在C++98的时候,就已经有了std::bind1st和std::bind2nd分别用来绑定functor的两个参数,具体代码就不演示了,查查资料就知道了。这个特性在当时并没有引起太多的重视,可以说是食之无味。
C++11中提供了std::bind
,可以说是一种飞跃的提升,bind本身是一种延迟计算的思想,它本身可以绑定普通函数、全局函数、静态函数、类静态函数甚至是类成员函数。
#include <iostream>
#include <functional>
using namespace std;
int TestFunc(int a, char c, float f)
{
cout << a << endl;
cout << c << endl;
cout << f << endl;
return a;
}
int main()
{
auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
bindFunc1(10);
cout << "=================================\n";
auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
bindFunc2('B', 10);
cout << "=================================\n";
auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
bindFunc3(100.1, 30, 'C');
return 0;
}
从上面的代码可以看到,bind能够在绑定时候就同时绑定一部分参数,未提供的参数则使用占位符表示,然后在运行时传入实际的参数值。
PS:绑定的参数将会以值传递的方式传递给具体函数,占位符将会以引用传递。
众所周知,静态成员函数其实可以看做是全局函数,而非静态成员函数则需要传递this指针作为第一个参数,所以std::bind
能很容易地绑定成员函数。
cocos2dx中的CC_CALLBACK
系列的宏其实就是一种封装好的std::bind
,它默认认为绑定的就是成员函数,帮助开发者简化了代码。
总结
bind
最终将会生成一个可调用对象,这个对象可以直接赋值给std::function
对象,而std::bind绑定的可调用对象可以是Lambda表达式或者类成员函数等可调用对象,这个是cocos2dx中的一般用法。它能随意绑定任何函数,将所有的函数都能统一到std::function
example:
#include <iostream>
#include <typeinfo>
#include <functional>
#include <string.h>
#include <iostream>
#include <typeinfo>
#include <functional>
#include <string.h>
using namespace std; int add(int a, int b, int c)
{
return a+b+c;
} class Utils{
public:
Utils(const char* name){
strcpy(_name, name);
} void SayHello(const char* name) const{
std::cout<<_name<<" say : hello "<<name<<endl;
} static int getId(){
return ;
} private:
char _name[];
}; int main()
{
auto add2 = std::bind(add, std::placeholders::_1, , );
int i=add2();
std::cout<<i<<std::endl;
std::cout<<typeid(add2).name()<<endl; cout<<"------------------------------------"<<endl; Utils util("xiaoming");
//绑定类成员
auto sayHello = std::bind(&Utils::SayHello, util, std::placeholders::_1);
sayHello("xiaodonmg"); auto SayHelloKit = std::bind(&Utils::SayHello, util, "kit");
SayHelloKit(); //绑定静态成员
auto getId = std::bind(&Utils::getId);
cout<<getId()<<endl; return ;
}
C++11中的std::bind的更多相关文章
- C++11中的std::function
看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...
- 【转】C++11中的std::function
原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...
- C++11 中function和bind以及lambda 表达式的用法
关于std::function 的用法: 其实就可以理解成函数指针 1. 保存自由函数 void printA(int a) { cout<<a<<endl; } std:: ...
- c++11中关于std::thread的join的思考
c++中关于std::thread的join的思考 std::thread是c++11新引入的线程标准库,通过其可以方便的编写与平台无关的多线程程序,虽然对比针对平台来定制化多线程库会使性能达到最大, ...
- 关于C++11中的std::move和std::forward
std::move是一个用于提示优化的函数,过去的c++98中,由于无法将作为右值的临时变量从左值当中区别出来,所以程序运行时有大量临时变量白白的创建后又立刻销毁,其中又尤其是返回字符串std::st ...
- [C/C++]关于C++11中的std::move和std::forward
http://www.cnblogs.com/cbscan/archive/2012/01/10/2318482.html http://blog.csdn.net/fcryuuhou/article ...
- c++11中关于`std::thread`线程传参的思考
关于std::thread线程传参的思考 最重要要记住的一点是:参数要拷贝到线程独立内存中,不管是普通类型.还是引用类型. 对于传递参数是引用类型,需要注意: 1.当指向动态变量的指针(char *) ...
- C++11中提供了std::bind
再来看看std::bind C++11中提供了std::bind.bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的. bind的思想实际上是一种延迟计算的思想,将可调用对象保存 ...
- C++11 中的function和bind、lambda用法
std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo ...
随机推荐
- selenium常用的API(三)获取网页title、html源码
获取网页title 获取页面title的方法可以直接用driver.title获取到,然后可以把获取到的结果用做断言. #encoding=utf-8 from selenium import web ...
- 第三天Beta冲刺
团队作业Beta冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 (1)掌握软件黑盒测试技术:(2)学会编制软件 ...
- DT6.0下搜索页解决canonical获取乱码问题
最近研究dt6.0,官方内核默认是把搜索页屏蔽的,但是做seo的人都知道,搜索页聚合是争取排名的好地方,所以我就二次开发搜索页,具体可以查看前几期分享的,今天说说关于搜索的canonical的url乱 ...
- Vue钩子函数
Vue的生命周期函数 beforeCreate:function(){ console.log('1-beforeCreate 初始化之后'); }, created:function(){ cons ...
- pl/sql developer 中文字段显示乱码 解决办法
一.原因:因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 二.查看和修改oracle数据库字符集: select userenv('language') from d ...
- [NgRx] NgRx Entity Adapter Configuration - Understanding sortComparer and selectId
import { Course, compareCourses } from "../model/course"; import { EntityState, createEnti ...
- am335x system upgrade rootfs for dhcpcd cross compile(十三)
dhcpcd移植 [目的] 移植dhcpcd的目是在AM335X开发板上使用dhcp功能,获取WAN口设备的IP,并且可以通过参数指定其matric,matric值越小,其优先级越高.如设备可以以太网 ...
- SPOJ简介。
今天一如既往地在luogu刷题,发现了一个新OJ,就去网上查资料.得到了下面这些. 以下转载自这里 SPOJ是波兰最为出色的Online Judge之一,界面和谐,题目类型也非常丰富,适合有一定基础的 ...
- (31)Vue安装
在使用Vue的时候,推荐安装Vue Devtools https://github.com/vuejs/vue-devtools#vue-devtools Browser devtools exten ...
- vue-router 的原理
1. hash 修改的时候:history.pushState('名字', null, '/xxx') || location.hash = '/xxx' 回退的时候:window.addEventL ...