C++11学习笔记之三lamda表达式,std::function, std::bind
2014-08-23 21:07
519 查看
//lamda
//first lamda
[]
{};
// second lamda
[]() //or no need () when paramater is null
{
std::cout << "second" << std::endl;
}();// last add(), express will call this lamda func
// 3 with return type
auto kkk = []()
{
return 1;
}();
// 4, set return type
auto kkkk = [] (int i) -> bool { return (bool)i; }(5);
//5, lamda capture, [=], [=,&], [&], [this][] 不捕获...
/*[&] 以引用的方式捕获
[=] 通过变量的一个拷贝捕获
[=, &foo] 通过变量的拷贝捕获,但是用foo变量的引用捕获
[bar] 通过复制捕获,不要复制其他
[this] 捕获this指针对应成员
*/
function, bind等,上代码
//first lamda
[]
{};
// second lamda
[]() //or no need () when paramater is null
{
std::cout << "second" << std::endl;
}();// last add(), express will call this lamda func
// 3 with return type
auto kkk = []()
{
return 1;
}();
// 4, set return type
auto kkkk = [] (int i) -> bool { return (bool)i; }(5);
//5, lamda capture, [=], [=,&], [&], [this][] 不捕获...
/*[&] 以引用的方式捕获
[=] 通过变量的一个拷贝捕获
[=, &foo] 通过变量的拷贝捕获,但是用foo变量的引用捕获
[bar] 通过复制捕获,不要复制其他
[this] 捕获this指针对应成员
*/
function, bind等,上代码
#include <iostream> #include <functional> using namespace std; typedef std::function<void()> fp; typedef std::function<void(int)> fpi1; void g_fun() { std::cout << "global function bind" << std::endl; } class A { public: static void aFunStatic() { std::cout << "aFunStatic" << std::endl; } virtual void aFun() { std::cout << "aFun" << std::endl; } void aFunI1(int i) { std::cout << "aFunI1:" << i << std::endl; } }; class DerivedA :public A { public: virtual void aFun() override { std::cout << "aFun in DeriveA" << std::endl; } }; class B { public: void bCallAFun() { if(callFun) callFun(); } void bCallAFunStatic() { if(callFunStatic) callFunStatic(); } void bCallAFunI1(int i) { if(callFunI1) callFunI1(i); } fp callFunStatic; fp callFun; fpi1 callFunI1; }; void main() { B b; A a; //bind to global function fp f = fp(&g_fun); f(); //bind to class function //static b.callFunStatic = std::bind(&A::aFunStatic); b.bCallAFunStatic(); //no static function without parameter b.callFun = std::bind(&A::aFun, &a); b.bCallAFun(); //no static function with parameter b.callFunI1 = std::bind(&A::aFunI1, &a, std::placeholders::_1); b.bCallAFunI1(5); //about polymorphic DerivedA da; b.callFun = std::bind(&A::aFun, &da); b.bCallAFun(); }
相关文章推荐
- C++11学习笔记6---std::function
- C++11 笔记 std::function,bind绑定器和lambda
- c++11基础学习(2)std::bind与std::function特性
- cocos2d-x-3.0学习笔记之std::function、std::bind
- C++11 学习笔记 std::function和bind绑定器
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- C++11 FAQ中文版:std::function 和 std::bind
- C++0x FAQ中文版:std::function 和 std::bind - [C++11 FAQ]
- C++11学习笔记:std::move和std::forward源码分析
- C++11 FAQ中文版:std::function 和 std::bind
- C++11新特性之二——std::bind std::function 高级用法
- 【C++11】新特性——function、bind以及lamda表达式
- C++11 FAQ中文版:std::function 和 std::bind
- C++11 FAQ中文版:std::function 和 std::bind
- 【C++11】新特性——std::function 和 std::bind
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- C++11 FAQ中文版:std::function 和 std::bind
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- C++11 std::bind std::function 高级用法