C++ 11: function & bind 使用示例
#include <functional>#include <iostream>struct Foo {Foo(int num) : num_(num) {}void print_add(int i) const { std::cout << num_+i << '\n'; }int num_;};void print_num(int i){std::cout << i << '\n';}struct PrintNum {void operator()(int i) const{std::cout << i << '\n';}};int main(){// store a free functionstd::function<void(int)> f_display = print_num;f_display(-9);// store a lambdastd::function<void()> f_display_42 = []() { print_num(42); };f_display_42();// store the result of a call to std::bindstd::function<void()> f_display_31337 = std::bind(print_num, 31337);f_display_31337();// store a call to a member functionstd::function<void(const Foo&, int)> f_add_display = &Foo::print_add;const Foo foo(314159);f_add_display(foo, 1);// store a call to a member function and objectusing std::placeholders::_1;std::function<void(int)> f_add_display2= std::bind( &Foo::print_add, foo, _1 );f_add_display2(2);// store a call to a function objectstd::function<void(int)> f_display_obj = PrintNum();f_display_obj(18);}
C++ 11: function & bind 使用示例的更多相关文章
- Using C++11 function & bind
The example of callback in C++11 is shown below. #include <functional> void MyFunc1(int val1, ...
- c++11 function bind 测试。
实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...
- C++ 类的成员函数指针 ( function/bind )
这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...
- 【转帖】漫话C++0x(四) —- function, bind和lambda
实在是觉得此文总是去翻感觉不太好.于是果断转过来了,想看原文的请戳:http://www.wuzesheng.com/?p=2032 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lam ...
- 【转】bind简单示例
bind简单示例代码 namespace { class placeholder_ {}; placeholder_ __1; } template <typename R, typename ...
- boost::bind应用示例
// testBind.cpp : Defines the entry point for the console application. // #include "stdafx.h&qu ...
- ES6下的Function.bind方法
在JavaScript的使用中,this的指向问题始终是一个难点.不同的调用方式,会使this指向不同的对象.而使用call,apply,bind等方式,可改变this的指向,完成一些令人惊叹的黑魔法 ...
- Extjs使用Ext.function.bind, 给句柄函数传参
回调函数updateImage中的key参数,在外部调用时有程序员自己指定. 使用Ext.Function.bind(this.updateImage, this, 'imageUrl', true) ...
- javascript 中 function bind()
Function bind() and currying <%-- All JavaScript functions have a method called bind that binds t ...
随机推荐
- C语言实现全排列和回溯法总结
一.递归实现全排列 #include"cstdio" ]; void print_permutation(int n,int *A,int cur){ if(cur==n){ ;i ...
- C语言中malloc函数与free函数
- malloc函数 全称是memory allocation,中文叫动态内存分配,用于申请一块连续的.指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑 ...
- IIS进程池异常崩溃,导致网站 service unavailable,原因排查与记录。
昨晚十点钟的样子,网站崩溃,开始 service unavailable,最近开始业务高峰,心里一惊,麻痹肯定进程池又异常崩溃了.又碰到什么问题?上次是因为一个异步线程的问题,导致了进程池直接崩溃,后 ...
- (转)Linux-HA实战(1)— Heartbeat安装
原文:http://blog.csdn.net/liaomin416100569/article/details/76087448-------centos7源代码编译安装heartbeat 原文:h ...
- 我的Python升级打怪之路【六】:面向对象(一)
面向对象的概述 面向过程:根据业务逻辑从上到下写代码 函数式:将其功能代码封装到函数中,日后便无需编写,仅仅调用即可 [执行函数] 面向对象:对函数进行分类和封装.[创建对象]==>[通过对象执 ...
- javascript正则表达式语法
1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...
- hadoop包含哪些技术?
1.Hadoop包含哪些技术?Common, Avro, MapReduce, HDFS, Pig, Hive, Hbase, ZooKeeper, Sqoop, Oozie. 2.简介Common: ...
- Visual Studio中判断项目的类型
1. 打开项目的属性,查看Application的Output type
- javascript高性能
我在<javascript高性能> 这本书里面读到这个文章,所以做一下学习笔记,供大家一块学习: 无阻塞脚本的概念什么? 为什么要用无阻塞脚本? 如何实现无阻塞脚本,和每个实现方式应该注意 ...
- Expression Blend实例中文教程(3) - 布局控件快速入门Grid
上一篇对Blend 3开发界面进行了快速入门介绍,本篇将基于Blend 3介绍Silverlight控件.对于微软开发工具熟悉的朋友,相信您很快就熟悉Blend的开发界面和控件. XAML概述 Sil ...