语义:

  类模板std::function是可调用对象的包装器,可以包装除了类成员之外的所有可调用对象。包括,普通函数,函数指针,lambda,仿函数。通过指定的模板参数,它可以用统一的方式保存,并延迟执行它们。所谓的延迟执行,就是回调了。

它使得C++对C的兼容性更强了。

常规多态案例:

#include <iostream>
#include <functional>
using namespace std;
class Operator
{
public:
    virtual int op(int,int) = ;
};
class OperatorAdd:public Operator
{
public:
    int op(int i,int j)
{
return i+j;
}
};
class OperatorMinus:public Operator
{
public:
    int op(int i,int j)
    {
      return i-j;
    }
};
int main()
{
  Operator *oper = new OperatorAdd;
  cout<<oper->op(,)<<endl;
  oper = new OperatorMinus;
  cout<<oper->op(,)<<endl;
  return ;
}

多态转std::function

#include <iostream>
#include <functional>
#include <map>
using namespace std;
int add(int i,int j)
{
return i+j;
}
int _minus(int i,int j)
{
return i-j;
} typedef int(*MINUS)(int,int);
//using MINUS = int(*)(int,int)

auto multiply = [](int i,int j){return i*j;}; class Divide
{
public:
int operator()(int i, int j){
return i/j;
}
};
int main()
{
std::function<int(int,int)> oper;
oper = add;
cout<<oper(,)<<endl;
MINUS m = _minus;
oper = m;
cout<<oper(,)<<endl;
oper = multiply;
cout<<oper(,)<<endl;
oper = Divide();
cout<<oper(,)<<endl;
map<string,std::function<int(int,int)>> math;
math.insert({"+",add});
math.insert({"-",_minus});
math.insert({"*",multiply});
math.insert({"/",Divide()});
math.insert({"%",[](int i, int j){return i%j;}});
cout<<math["+"](,)<<endl;
cout<<math["-"](,)<<endl;
cout<<math["*"](,)<<endl;
cout<<math["/"](,)<<endl;
cout<<math["%"](,)<<endl;
return ;
}

  写完这段代码完全被震撼了,被感动的不要不要的。C++的灵活性简直逆天了。

应用:

常规回调

#include <iostream>
#include <functional>
using namespace std;
class functor
{
public:
void operator()()
{
cout<<__FUNCTION__<<endl;
}
}; class A
{
public:
A(const function<void()> & cb):_callback(cb)
{}
void notify()
{
_callback();
}
function<void()> _callback;
};
int main(int argc, char *argv[])
{
functor fct;
A a(fct);
a.notify();
return ;
}
#include <iostream>
#include <functional> using namespace std; void printWhenEven(int data,const std::function<void(int)> &f)
{
if(data%)
f(data);
} void print(int i)
{
cout<<i<<endl;
} int main()
{
for(int i =;i<;i++)
{
printWhenEven(i,print);
cout<<"+++++++++++++++++++"<<endl;
// printWhenEven(i,[](int i){cout<<i+1<<endl;});
}
}

std::function"函数"对象包装器的更多相关文章

  1. C++11多态函数对象包装器

    [C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...

  2. c++11——std::function和bind绑定器

    c++11中增加了std::function和std::bind,可更加方便的使用标准库,同时也可方便的进行延时求值. 可调用对象 c++中的可调用对象存在以下几类: (1)函数指针 (2)具有ope ...

  3. C++11 学习笔记 std::function和bind绑定器

    C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...

  4. Java基础(八)对象包装器与自动装箱

    1.对象包装器 有时候,需要将int这样的基本类型转换为对象.所有的基本类型都有一个与之对应的类.通常,这些类被称为包装器(wrapper). 这些对象包装类分别是:Integer.Long.Floa ...

  5. 第12课 std::bind和std::function(3)_std::function可调用对象包装器

    1. std::function (1)首先是一个类模板,用于包装可调用对象.可以容纳除了类成员(函数)指针之外的所有可调用对象. (2)可以将普通函数,lambda表达式和函数对象类统一起来.尽管它 ...

  6. 关于Function()函数对象的那些小九九

    概念:首先,函数是一种特殊类型的数据,函数也是数据类型的一种,实际上函数也是一种对象,函数对象的内建构造器是Function(); 函数的几种创建方式: 函数声明法: function sum(a,b ...

  7. python3 functools partial 用于函数的包装器详解

    一.partial 的作用: partial 用于对一个已有函数进行包装,达到功能的定制的目的. 二.例子: 假设我们要完成两个功能,第一个功能是完成两个数相加,第二个功能是给一个自增一下 1.传统方 ...

  8. 剖析std::function接口与实现

    目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...

  9. C++11新特性之八——函数对象function

    详细请看<C++ Primer plus>(第六版中文版) http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html ...

随机推荐

  1. 我的Android进阶之旅------>解决Your project contains error(s),please fix them

    在使用eclipse写好Android的代码,代码没有报错.然后想在AVD中运行测试时,弹出错误框,提示信息为:  "Your project contains error(s),pleas ...

  2. 如何使用doctrine:migrations:migrate

    doctrine:migrations:migrate: 可以生成数据库表 当新建完实体之后需要执行 doctrine:migrations:diff 更新差异到db 然后就ok了,这时候你的app/ ...

  3. 如何修改硬盘挂载的名字LABEL

    ➜ ~ df -h Filesystem Size Used Avail Use% Mounted on/dev/sda2 114G 97G 12G 90% /media/brian/4ef34b75 ...

  4. 在pycharm中执行脚本没有报错但输出显示Redirection is not supported.

    没有新式语法错误,但是输出显示Redirection is not supported.(不支持重定向) 在stockflow中找到是因为从IDE中运行脚本的原因,比如pycharm,所有IDE都提供 ...

  5. ubuntu部分常用操作指令记录

    # 以ROOT权限打开图形文件管理界面: sudo nautilus # 给某个文件添加可执行权限,例如: sudo chmod +x /usr/lib/jdk/bin/java # 修改某个文件或文 ...

  6. kvm初体验之一:参考文档

    KVM Virtualization in RHEL 6 Made Easy KVM Virtualization in RHEL 6 Made Easy – Part 2 RHEL 6 Virtua ...

  7. Seq2SQL :使用强化学习通过自然语言生成SQL

    论文: https://einstein.ai/static/images/layouts/research/seq2sql/seq2sql.pdf  数据集:https://github.com/s ...

  8. JSP常见知识点

    false 7.8 磅 0 2 false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTa ...

  9. nginx使用ssl模块配置HTTPS支持 <转>

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  10. 对python生成器特性使用的好例子

    1.对序列进行分组的函数(摘自web.py源码utils.py文件中) def group(seq, size): """ Returns an iterator ove ...