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. 约瑟夫环问题及python与c++实现效率对比

    约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重 ...

  2. jodd-StringTemplateParser使用

    StringTemplateParser 时一个string模板的解析器.在string模板中定义类似jsp标签的宏. 在解析过程中,宏被对值替换,值通过自定义的MacroResolver解析得到. ...

  3. spring data redis使用示例

    1. 配置依赖文件 <dependencies> <dependency> <groupId>org.springframework.data</groupI ...

  4. 架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层

    1.概述 前两遍文章中,我们一直在说后文要介绍Nginx + Keepalived的搭建方式.这篇文章开始,我们就来兑现前文的承诺,后续的两篇文章我们将介绍Nginx + Keepalived和 LV ...

  5. 基于xml的用户注册登录案例

    用户注册登录 要求:3层框架,使用验证码 1        功能分析 l  注册 l  登录 1.1 JSP页面 l  regist.jsp 注册表单:用户输入注册信息: 回显错误信息:当注册失败时, ...

  6. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

  7. Java SE ---流程控制语句

     java的控制流程有三种: 一,顺序流程             自上而下,按照代码的先后顺序执行 二,分支流程             1,if/else语句             2,swit ...

  8. FVDI Commander products be replaced SVDI tools,really?

    You may have heard that some FVDI Commander products are being replaced by the new SVDI tools. This ...

  9. OnTouchListener事件监听实现方式之GestureDetector

    当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等. 一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouc ...

  10. Android读取RAM,ROM,SD卡容量

    1)简介 一般人们在买手机的时候,在手机配置上都会出现"内存容量:512MB ROM+512MB RAM "等等类似这样的说明,可能很多人都知道RAM的意思就是运存的意思,但是对于 ...