C++11中std::bind的使用】的更多相关文章

std::bind: Each argument may either be bound to a value or be a placeholder: (1).If bound to a value, calling the returned function object will always use that value as argument; (2).If a placeholder, calling the returned function object forwards an…
class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any Callable target -- functions,lambda expressions, bind expressions, or other function objects, as well as pointe…
note 更多用法,请参考: cppreference 用的少,容易忘. 我的理解 类似延迟计算. 比如,回调函数,将回调函数传入后,回调函数不一定马上被调用. 它是一个模板类,调用后将生成一个新的调用对象A.调用该对象A与调用原函数是等价的. 声明 截至目前,它的声明如下 需要包含头文件 #include <functional> 一个例子 代码 下面的print函数负责输出参数的值, 通过使用std::bind, 生成一个新的对象 func, 此时, func(a, b, c);与prin…
std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular,std::move produces an xvalue expression that identifies its argument t. It is exactly e…
1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. (2)函数的返回值是一个函数对象.它用于包装和改造传入的函数(或仿函数),并形成一个新的仿函数对象(是一个可调用对象). (3)与旧的可调用对象相比,新的仿函数对象参数个数少了1个.(相当于给少掉的那个参数一个默认的值) 2. std::bind绑定器 (1)首先,std::bind也是一个函数模板,…
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg is an lvalue reference, the function returns arg without modifying its type. std::forward:This is a helper function to allow perfect forwarding of arg…
unordered map is an associative container that contains key-value pairs with unique keys. Search, insertion, and removal of elements have average constant-time complexity. Internally, the elements are not sorted in any particular order,but organized…
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg is an lvalue reference, the function returns arg without modifying its type. std::forward:This is a helper function to allow perfect forwarding of arg…
关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就不会再被使用了.针对这两种值引入了左值引用和右值引用,以及引用折叠的概念. 1.右值引用的举例测试 #include <iostream> using namespace std; ​ //创建一个测试类 class A { public: A() : m_a() { } ​ int m_a; }…
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比较低层次的东西,有时候使用有些不便,比如我希望获取线程函数的返回结果的时候,我就不能直接通过thread.join()得到结果,这时就必须定义一个变量,在线程函数中去给这个变量赋值,然后join,最后得到结果,这个过程是比较繁琐的.c++11还提供了异步接口std::async,通过这个异步接口可以…
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比较低层次的东西,有时候使用有些不便,比如我希望获取线程函数的返回结果的时候,我就不能直接通过 thread.join()得到结果,这时就必须定义一个变量,在线程函数中去给这个变量赋值,然后join,最后得到结果,这个过程是比较繁琐的. c++11还提供了异步接口std::async,通过这个异步接口…
引子 最近群里比较热闹,大家都在山寨c++11的std::bind,三位童孩分别实现了自己的bind,代码分别在这里: 木头云的实现 mr.li的实现 null的实现,null的另一个版本的实现 这些实现思路和ms stl的std::bind的实现思路是差不多的,只是在实现的细节上有些不同.个人觉得木头云的实现更简洁,本文中的简单实现中select函数用的是木头云的,在此表示感谢.下面我们来分析一下bind的基本原理. bind的基本原理 bind的思想实际上是一种延迟计算的思想,将可调用对象保…
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + ); t.join(); 但是线程毕竟是属于比较低层次的东西,有时候使用有些不便,比如我希望获取线程函数的返回结果的时候,我就不能直接通过thread.join()得到结果,这时就必须定义一个变量,在线程函数中去给这个变量赋值,然后join,最后得到结果,这个过程是比较繁琐的.c++11还提供了异步接口std::async,通过这个异步接口可以很…
一. std::bind (一)std::bind实现的关键技术 [编程实验]探索bind原理,实现自己的bind函数 #include <iostream> #include <tuple> using namespace std; //1. 占位符定义 template<size_t idx> struct placeholder{}; template<size_t idx> using ph = placeholder<idx>; con…
本文主要介绍了C++11中的移动语义与右值引用, 并且对其中的一些坑做了深入的讨论. 在正式介绍这部分内容之前, 我们先介绍一下rule of three/five原则, 与copy-and-swap idiom最佳实践. 本文参考了stackoverflow上的一些回答. 不能算是完全原创 rule of three/five rule of three是自从C++98标准问世以来, 大家总结的一条最佳实践. 这个实践其实很简单, 用一句话就能说明白: 析构函数, 拷贝构造函数, =操作符重载…
再来看看std::bind C++11中提供了std::bind.bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的. bind的思想实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用.而且这种绑定是非常灵活的,不论是普通函数.函数对象.还是成员函数都可以绑定,而且其参数可以支持占位符,比如你可以这样绑定一个二元函数auto f = bind(&func, _1, _2);,调用的时候通过f(,)实现调用. 简单的认为就是std::bind就是std:…
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__, ##__V…
看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased; 这两行代码是从Cocos2d-x中摘出来的,重点是这两行代码的定义啊.std::function这是什么东西?如果你对上述两行代码表示毫无压力,那就不妨再看看本文,就当温故而知新吧…
std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std::cout #include<functional>// std::function void func(void) { std::cout << __FUNCTION__ << std::endl; } class Foo { public: static int fo…
在c++11中,一个callable object(可调用对象)可以是函数指针.lambda表达式.重载()的某类对象.bind包裹的某对象等等,有时需要统一管理一些这几类对象,新增的function就是为此而生.function位于functional头文件,可以看做是那几类callable object的抽象表示. #include<iostream> #include<functional> using namespace std; int f_add(int i,int j…
原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased; 这两行代码是从Cocos2d-x中摘出来的,重点是这两行代码的定义啊.std::functi…
std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo { void print_sum(int n1, int n2) { std::cout << n1 + n2 << '\n'; } ; }; int main() { Foo foo; auto f = std::bind(&Foo::print_sum, &foo,…
std::function是可调用对象的包装器:std::bind是将可点用对象和其参数一起进行绑定,且绑定后的结果可以使用std::function对象进行保存,并延迟调用到需要调用的时候: 在C++中,可调用实体主要包括函数,函数指针,函数引用,可以隐式转换为函数指定的对象,或者实现了opetator()的对象(即C++98中的functor).C++0x中,新增加了一个std::function对象,std::function对象是对C++中现有的可调用实体的一种类型安全的包裹(我们知道像…
cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码: auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 2.0内的代码用的不是CC_CALLBACK_1而是menu_selector. CC_CALLBACK系列是3.…
昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0内的Label锚点是在ccp(0.5,0.5),而一直3.0rc0是ccp(0,0). 累觉不爱.尽管cocos2d-x改变太快,兼容性一次次的暴露出不足,但是,总归是向好的方向进行.于是下载了3.0来玩玩~ cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码:…
std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std::cout #include<functional>// std::function void func(void) { std::cout << __FUNCTION__ << std::endl; } class Foo { public: static int fo…
关于std::function 的用法:  其实就可以理解成函数指针 1. 保存自由函数 void printA(int a) { cout<<a<<endl; } std::function<void(int a)> func; func = printA; func(2); 2.保存lambda表达式 std::function<void()> func_1 = [](){cout<<"hello world"<&l…
参考博客: C++可调用对象详解-https://www.cnblogs.com/Philip-Tell-Truth/p/5814213.html 一.关于std::function与std::bind 翻看了几篇博客,还不如看书逻辑性好.以下内容摘自祁宇<深入应用C++11: 代码优化与工程级应用>一书. 有以下4种情况可被称为可调用对象. 1.是一个函数指针.(这里的函数要求是如C函数一样的编译期内存确定的函数指针) 2.是一个具有operator()成员函数的类对象.(让类重载调用操作符…
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. 如今就算是补充吧,再把std::bind进行讨论讨论. 何为Callable Objects? 就可以调用对象,比方函数指针.仿函数.类成员函数指针等都可称为可调用对象. 对象包装器 Function wrapper Class that can wrap any kind of callable eleme…
目录 std::function可调用对象包装器 std::function基本用法 std::function/std::bind与抽象工厂.工厂方法的一点思考 std::function可调用对象包装器 C++可调用对象(Callable Objects)定义如下: 函数指针:与C语言一致: 类成员函数指针: 仿函数(functor):也成函数对象,重载operator()运算符的类/结构体对象: lambda表达式. std::function是Callable Objects的包装器(W…