function是函数、函数对象、函数指针、和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针

普通函数

#include <functional>
void print_num(int i)
{
cout << "i" << endl;
} function<void(int)> f_display = print_num;
f_display(-); function<void()> f_display_42 = [](){ print_num(); };
f_display_42(); function<void()> f_display_32337 = bind(print_num, ); //对bind不明白参考bind
f_display_32337();

类成员函数

#include <functional>
stuct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { cout << num_ + i << endl;}
int num_;
}; function<void(const Foo&, int)> f_add_display = &Foo::print_add;
const Foo foo();
f_add_display(foo, ); function<void(int)> f_add_display2 = bind(&Foo::print_add, foo, placeholders::_1);
f_add_display2();

一个实际的应用:

   #include <functional>
#include <iostream> using namespace std; class Scope {
public:
explicit Scope(function<void()> o) :
on_exit_(o) {}
~Scope() { on_exit_(); }
private:
function<void()> on_exit_;
}; int main()
{
Scope scope([]() { cout << "close" << endl; });
}

输出结果: close

c++11:function的用法的更多相关文章

  1. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  2. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  3. c/c++ 重载运算符 标准库function的用法

    重载运算符 标准库function的用法 问题:int(int, int)算不算一种比较通用的类型?? 比如函数: int add(int a, int b); 比如lambda:auto mod = ...

  4. python3新特性函数注释Function Annotations用法分析

    本文分析了python3新特性函数注释Function Annotations用法.分享给大家供大家参考,具体如下: Python 3.X新增加了一个特性(Feature),叫作函数注释 Functi ...

  5. C++11 function用法 可调用对象模板类

    std::function<datatype()> ()内写参数类型 datatype 代表function的返回值 灵活的用法.. 代码如下 #include <stdio.h&g ...

  6. C++11新特性之二——std::bind std::function 高级用法

    /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #includ ...

  7. ECharts中color : function的用法(转)

    ECharts图表实战经验1:如何设置图表同序列不同数据点的独立颜色值   最近有不少朋友在追问这样一个问题:我单序列的柱状图,我想让每一个根柱子的颜色都不一样,应该如何做? 针对这个问题,其实我只想 ...

  8. C++11 Function 使用场景

    [1]场景分析 在一个函数内部,可能会多次用到某一段代码,一般情况是把这段用到次数较多的代码封装成一个函数. 但是,如果这段代码仅仅只在这个函数中有使用,这时封装成函数显得既麻烦又冗赘. 那么,有没有 ...

  9. Using C++11 function & bind

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

随机推荐

  1. 548 - Tree (UVa OJ)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  2. Java基础知识强化105:打印数组的方法总结

    1. 使用for循环打印数组. 2. 使用Arrays工具类,将数组转化为有序的List打印出来. 3. 使用Arrays工具类,使用Arrays.toString()输出数组内容. 上面三种方法打印 ...

  3. Crosswalk入门

    Crosswalk入门 CSDN资讯:Crosswalk的介绍 Crosswalk官方地址 上面的链接可以看到Crosswalk的介绍,Crosswalk种种吹牛逼的描述我就不写了.写一下我的使用感受 ...

  4. ubuntu下安装redis

    (1)进去 /usr/local目录下   cd /usr/local  若没有local这个文件夹则创建一个    sudo mkdir /usr/local     sudo chmod 777 ...

  5. js的时间操作方法

    1.js获取系统时间格式为YYYY-MM-DD HH:MM:SS 1 function curDateTime(){ 2 var d = new Date(); 3 var year = d.getY ...

  6. 教您如何检查oracle死锁,决解死锁

    oracle死锁问题一直困扰着我们,下面就教您一个oracle死锁的检查方法,如果您之前遇到过oracle死锁方面的问题,不妨一看…… oracle死锁问题一直困扰着我们,下面就教您一个oracle死 ...

  7. Lnmp下安装memcached

            Lnmp下安装memcached 1.先安装 libevent,再安装 Memcached主程序 # tar xf libevent-2.0.21-stable.tar.gz # cd ...

  8. linux yum install resource - epel

    首先现在如下rpm包,然后安装对应的rpm包centos5 32位epel源下载地址: www.lishiming.net/data/attachment/forum/epel-release-5-4 ...

  9. [转]oracle 11g 忘记 默认用户密码

    本文转自:http://blog.csdn.net/huangbiao86/article/details/6595052 首先启动sqlplus 输入用户名:sqlplus / as sysdba ...

  10. BZOJ 1040: [ZJOI2008]骑士 基环加外向树

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1190  Solved: 465[Submit][Status] ...