std::function 是一个模板类,用于封装各种类似于函数这样的对象,例如普通函数,仿函数,匿名函数等等。其强大的多态能力,让其使用只依赖于调用特征。在程序的升级中,可以实现一个调用表,以兼容新旧不同的实现方式。

例如:

 #include <iostream>
#include <functional>
#include <list> using namespace std; //普通函数
int c_fun_add(int a, int b)
{
return a + b;
} //模板函数
template <typename T>
T t_fun_add(T a, T b)
{
return a + b;
} //仿函数
class functor_add
{
public:
int operator()(int a, int b)
{
return a + b;
}
}; //匿名函数,lambda表达式
auto l_fun_add = [](int a, int b)
{
return a + b;
}; //成员函数
class FunClass
{
public:
static int s_fun_add(int a, int b)
{
return a + b;
} int n_fun_add(int a, int b)
{
return a + b;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
functor_add functor;
FunClass funcls;
std::list<std::function<int(int, int)>> call_tables;
call_tables.push_back(c_fun_add);
call_tables.push_back(t_fun_add<int>);
call_tables.push_back(functor);
call_tables.push_back(l_fun_add);
call_tables.push_back(funcls.s_fun_add);
call_tables.push_back(bind(&FunClass::n_fun_add, &funcls, placeholders::_1, placeholders::_2)); for (const auto& fun : call_tables)
{
cout << fun(, ) << endl;
}
getchar();
return ;
}

c++ std::function的更多相关文章

  1. C++11中的std::function

    看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...

  2. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  3. typedef 函数指针 数组 std::function

    1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA,  *PDATA;  //D ...

  4. callable object与新增的function相关 C++11中万能的可调用类型声明std::function<...>

    在c++11中,一个callable object(可调用对象)可以是函数指针.lambda表达式.重载()的某类对象.bind包裹的某对象等等,有时需要统一管理一些这几类对象,新增的function ...

  5. std::bind和std::function

    std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数,特别是在多线程的程序中,经常用它将函数进行包装,然后打包发送给工作线程,让工作线程去执行我们的任务. ...

  6. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  7. C++11 std::function用法

    转自 http://www.hankcs.com/program/cpp/c11-std-function-usage.html function可以将普通函数,lambda表达式和函数对象类统一起来 ...

  8. 【转】C++11中的std::function

    原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...

  9. std::function,std::bind复习

    #include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...

  10. C++中的仿函数,std::function和bind()的用法

    1.仿函数:又叫std::function,是C++中的一个模板类 2.C语言中的函数指针: int  add(int a,int b) { return a+b; } typedef int (*f ...

随机推荐

  1. NRF51822/NRF51802/NRF52810/NRF52811的详解区别

    nRF51系列 - 多协议低功耗蓝牙和ANT/ANT+ 和2.4GHz专用系统级芯片 NRF51822-QFAA和NRF51802-QFAA在FLASH RAM的容量没有差别:区别在于:1.接收灵敏度 ...

  2. scapy学习笔记(5)

    1.ACK Scan >>>ans,unans=sr(IP(dst=,],flags="A") 扫描后,若要找出未过虑的端口: for s,r in ans: i ...

  3. Jmeter之目录结构

    首先得了解一下这些东西,以后才能快速的找到某些配置文件进行修改(举个例子,改配置只是其中之一) 一.bin目录examples:  目录中有CSV样例 jmeter.bat  windows的启动文件 ...

  4. Foxmail添加gmail密码错误

    想在foxmail上添加gmail时一直报密码错误,找了一圈发现是开启了两步验证,需要用应用专用密码才可以登录,生成应用专用密码的地址如下: https://security.google.com/s ...

  5. 为什么重写equals还要重写hashcode??

    equals和hashcode是object类下一个重要的方法,而object类是所有类的父类,所以所有的类都有这两个方法 equals和hashcode间的关系: 1.如果两个对象相同(即equal ...

  6. 使用Gitblit 在Windows2008 r2上部署Git Server(完整版)

    第一章 前言 使用gitblit搭建git server需要配置两个大件:java jdk.gitblit 本次搭建gitServer采用是当前最新版的两个版本,分别如下: jdk:Java SE D ...

  7. Redis数据库的安装与基本应用

    一:了解NoSQL 1:介绍:Nosql的全称是Not Only Sql,这个概念早起就有人提出,在09年的时候比较火.Nosql指的是非关系型数据库,而我们常用的都是关系型数据库.就像我们常用的my ...

  8. 1.5《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——清屏

    使用命令行时,使用clear命令清除屏幕非常方便: $ clear 键盘简写为^L.(Ctrl + L) 同样地,使用完终端当前窗口或标签页,使用exit命令退出进程: $ exit 键盘简写为^D ...

  9. 20155213免考项目——bof进阶及简易的HIDAttack

    20155213免考项目--bof进阶及简易的HIDAttack 目录 序 任务一:构造Shellcode(64位) 任务二:64位Shellcode的注入 任务三:32位及64位bof攻击(开启堆栈 ...

  10. VS新建一个模板工程

    新建一个模板工程的好处:    1.就不用每次都走一边新建向导了,新建工程一步到位. 2.可以往项目中每次都的输入的代码,如一些声明注释-- 效果图: 具体步骤: 1.自己先新建一个属于自己的工程. ...