参考资料


• cplusplus.comhttp://www.cplusplus.com/reference/functional/function/

• cppreference.comhttp://en.cppreference.com/w/cpp/utility/functional/function

std::function简介


• 类模板声明

// MS C++ 2013
template<class _Fty> class function;
template<class _Fty> class function : public _Get_function_impl<_Fty>::type { ... } // GCC 4.8.2
template<typename _Signature> class function;
template<typename _Res, typename... _ArgTypes> class function<_Res(_ArgTypes...)>
: public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, private _Function_base { ... } // cplusplus.com
template <class T> function; // undefined
template <class Ret, class... Args> class function<Ret(Args...)>;

• 类模板说明

std::function是一个函数包装器模板,最早来自boost库,对应其boost::function函数包装器。在c++0x11中,将boost::function纳入标准库中。该函数包装器模板能包装任何类型的可调用元素(callable element),例如普通函数和函数对象。包装器对象可以进行拷贝,并且包装器类型仅仅只依赖于其用特征(call signature),而不依赖于可调用元素自身的类型。

一个std::function类型对象实例可以包装下列这几种可调用元素类型:函数、函数指针、类成员函数指针或任意类型的函数对象(例如定义了operator()操作并拥有函数闭包)。std::function对象可被拷贝和转移,并且可以使用指定的调用特征来直接调用目标元素。当std::function对象未包裹任何实际的可调用元素,调用该std::function对象将抛出std::bad_function_call异常

• 模板参数说明

cplusplus.com中描述的原型说明:

T      : 通用类型,但实际通用类型模板并没有被定义,只有当T的类型为形如Ret(Args...)的函数类型才能工作。

Ret   : 调用函数返回值的类型。

Args : 函数参数类型。

std::function详解


• 包装普通函数

#include <iostream>
#include <functional>
using namespace std; int g_Minus(int i, int j)
{
return i - j;
} int main()
{
function<int(int, int)> f = g_Minus;
cout << f(, ) << endl; // -1
return ;
}

• 包装模板函数

#include <iostream>
#include <functional>
using namespace std; template <class T>
T g_Minus(T i, T j)
{
return i - j;
} int main()
{
function<int(int, int)> f = g_Minus<int>;
cout << f(, ) << endl; // -1
return ;
}

• 包装lambda表达式

#include <iostream>
#include <functional>
using namespace std; auto g_Minus = [](int i, int j){ return i - j; }; int main()
{
function<int(int, int)> f = g_Minus;
cout << f(, ) << endl; // -1
return ;
}

• 包装函数对象

非模板类型:

#include <iostream>
#include <functional>
using namespace std; struct Minus
{
int operator() (int i, int j)
{
return i - j;
}
}; int main()
{
function<int(int, int)> f = Minus();
cout << f(, ) << endl; // -1
return ;
}

模板类型:

#include <iostream>
#include <functional>
using namespace std; template <class T>
struct Minus
{
T operator() (T i, T j)
{
return i - j;
}
}; int main()
{
function<int(int, int)> f = Minus<int>();
cout << f(, ) << endl; // -1
return ;
}

• 包装类静态成员函数

非模板类型:

#include <iostream>
#include <functional>
using namespace std; class Math
{
public:
static int Minus(int i, int j)
{
return i - j;
}
}; int main()
{
function<int(int, int)> f = &Math::Minus;
cout << f(, ) << endl; // -1
return ;
}

模板类型:

#include <iostream>
#include <functional>
using namespace std; class Math
{
public:
template <class T>
static T Minus(T i, T j)
{
return i - j;
}
}; int main()
{
function<int(int, int)> f = &Math::Minus<int>;
cout << f(, ) << endl; // -1
return ;
}

• 包装类对象成员函数

非模板类型:

#include <iostream>
#include <functional>
using namespace std; class Math
{
public:
int Minus(int i, int j)
{
return i - j;
}
}; int main()
{
Math m;
function<int(int, int)> f = bind(&Math::Minus, &m, placeholders::_1, placeholders::_2);
cout << f(, ) << endl; // -1
return ;
}

模板类型:

#include <iostream>
#include <functional>
using namespace std; class Math
{
public:
template <class T>
T Minus(T i, T j)
{
return i - j;
}
}; int main()
{
Math m;
function<int(int, int)> f = bind(&Math::Minus<int>, &m, placeholders::_1, placeholders::_2);
cout << f(, ) << endl; // -1
return ;
}

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. iOS强制横屏或强制竖屏

    原文链接 https://www.jianshu.com/p/d6cb54d2eaa1 亲测第二种我这边是阔以滴 第一种解决方案(不推荐,直接跳过看第二种解决方案): //强制转屏 - (void)i ...

  2. halcon开发必读

    关于HALCON的新手入门问题简答(1) 无论读入什么图像,读入图像显示效果明显和原始图像不一致,哪怕是从相机读入的图像,也是明显颜色差异.什么原因引起? 答:初步诊断是,显示的时候调用的颜色查找表存 ...

  3. linux技巧---为各应用创建快捷方式

    linux中启动或关闭应用有时候比较麻烦,你必须cd到该应用的可执行脚本的目录中再执行该脚本,不能在任意目录下开启或关闭应用..当然,设置了环境变量path可以解决在任意目录下开启应用的问题,但是每个 ...

  4. 利用final定义方法:这样的方法为一个不可覆盖的方法。

      常量(这里的常量指的是实例常量:即成员变量)赋值: ①在初始化的时候通过显式声明赋值.Final int x=3: ②在构造的时候赋值. 局部变量可以随时赋值.   利用final定义方法:这样的 ...

  5. 64位 windows10,安装配置MYSQL8.0.13

    MySQL的安装配置过程,一查网上一大堆,但是每个人在安装配置的过程中都会碰到一些问题,因为安装的版本不一样,有些命令可能就不适用了.所以安装之前一定先确认好你的版本号. 下面开始安装MYSQL8.0 ...

  6. 如何查看github排行热度

    github热门趋势 https://github.com/trending github star排行榜 github输入:star:>数字,来查看star数的仓库: 输入:location: ...

  7. 入围T00ls 2018风云人物

    今天早上打开T00ls,发现成功入围了<T00ls第六届(2018)年度人物风云榜>,共34名年度人物,每个id可投10票,34选10. T00ls是当前国内为数不多的民间网络信息安全研究 ...

  8. 异步任务神器 Celery-入门

    一.Celery入门介绍 在程序的运行过程中,我们经常会碰到一些耗时耗资源的操作,为了避免它们阻塞主程序的运行,我们经常会采用多线程或异步任务.比如,在 Web 开发中,对新用户的注册,我们通常会给他 ...

  9. Qt编写自定义控件2-进度条标尺

    前言 进度条标尺控件的应用场景一般是需要手动拉动进度,上面有标尺可以看到当前进度,类似于qslider控件,其实就是qslider+qprogressbar的杂交版本,不过我才用的是纯qpainter ...

  10. HBase多条件及分页查询的一些方法

    HBase是Apache Hadoop生态系统中的重要一员,它的海量数据存储能力,超高的数据读写性能,以及优秀的可扩展性使之成为最受欢迎的NoSQL数据库之一.它超强的插入和读取性能与它的数据组织方式 ...