#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 function
    std::function<void(int)> f_display = print_num;
    f_display(-9);

    // store a lambda
    std::function<void()> f_display_42 = []() { print_num(42); };
    f_display_42();

    // store the result of a call to std::bind
    std::function<void()> f_display_31337 = std::bind(print_num, 31337);
    f_display_31337();

    // store a call to a member function
    std::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 object
    using 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 object
    std::function<void(int)> f_display_obj = PrintNum();
    f_display_obj(18);
}

C++ 11: function & bind 使用示例的更多相关文章

  1. Using C++11 function & bind

    The example of callback in C++11 is shown below. #include <functional> void MyFunc1(int val1, ...

  2. c++11 function bind 测试。

    实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...

  3. C++ 类的成员函数指针 ( function/bind )

    这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...

  4. 【转帖】漫话C++0x(四) —- function, bind和lambda

    实在是觉得此文总是去翻感觉不太好.于是果断转过来了,想看原文的请戳:http://www.wuzesheng.com/?p=2032 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lam ...

  5. 【转】bind简单示例

    bind简单示例代码 namespace { class placeholder_ {}; placeholder_ __1; } template <typename R, typename ...

  6. boost::bind应用示例

    // testBind.cpp : Defines the entry point for the console application. // #include "stdafx.h&qu ...

  7. ES6下的Function.bind方法

    在JavaScript的使用中,this的指向问题始终是一个难点.不同的调用方式,会使this指向不同的对象.而使用call,apply,bind等方式,可改变this的指向,完成一些令人惊叹的黑魔法 ...

  8. Extjs使用Ext.function.bind, 给句柄函数传参

    回调函数updateImage中的key参数,在外部调用时有程序员自己指定. 使用Ext.Function.bind(this.updateImage, this, 'imageUrl', true) ...

  9. javascript 中 function bind()

    Function bind() and currying <%-- All JavaScript functions have a method called bind that binds t ...

随机推荐

  1. C语言实现全排列和回溯法总结

    一.递归实现全排列 #include"cstdio" ]; void print_permutation(int n,int *A,int cur){ if(cur==n){ ;i ...

  2. C语言中malloc函数与free函数

    - malloc函数 全称是memory allocation,中文叫动态内存分配,用于申请一块连续的.指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑 ...

  3. IIS进程池异常崩溃,导致网站 service unavailable,原因排查与记录。

    昨晚十点钟的样子,网站崩溃,开始 service unavailable,最近开始业务高峰,心里一惊,麻痹肯定进程池又异常崩溃了.又碰到什么问题?上次是因为一个异步线程的问题,导致了进程池直接崩溃,后 ...

  4. (转)Linux-HA实战(1)— Heartbeat安装

    原文:http://blog.csdn.net/liaomin416100569/article/details/76087448-------centos7源代码编译安装heartbeat 原文:h ...

  5. 我的Python升级打怪之路【六】:面向对象(一)

    面向对象的概述 面向过程:根据业务逻辑从上到下写代码 函数式:将其功能代码封装到函数中,日后便无需编写,仅仅调用即可 [执行函数] 面向对象:对函数进行分类和封装.[创建对象]==>[通过对象执 ...

  6. javascript正则表达式语法

    1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...

  7. hadoop包含哪些技术?

    1.Hadoop包含哪些技术?Common, Avro, MapReduce, HDFS, Pig, Hive, Hbase, ZooKeeper, Sqoop, Oozie. 2.简介Common: ...

  8. Visual Studio中判断项目的类型

    1. 打开项目的属性,查看Application的Output type

  9. javascript高性能

    我在<javascript高性能> 这本书里面读到这个文章,所以做一下学习笔记,供大家一块学习: 无阻塞脚本的概念什么? 为什么要用无阻塞脚本? 如何实现无阻塞脚本,和每个实现方式应该注意 ...

  10. Expression Blend实例中文教程(3) - 布局控件快速入门Grid

    上一篇对Blend 3开发界面进行了快速入门介绍,本篇将基于Blend 3介绍Silverlight控件.对于微软开发工具熟悉的朋友,相信您很快就熟悉Blend的开发界面和控件. XAML概述 Sil ...