转自:

https://www.cnblogs.com/heartchord/p/5017071.html

////////////////////

std::function

 

参考资料


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

• cppreference.com:http://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(1, 2) << endl; // -1
return 1;
}

• 包装模板函数

#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(1, 2) << endl; // -1
return 1;
}

• 包装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(1, 2) << endl; // -1
return 1;
}

• 包装函数对象

非模板类型:

#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(1, 2) << endl; // -1
return 1;
}

模板类型:

#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(1, 2) << endl; // -1
return 1;
}

• 包装类静态成员函数

非模板类型:

#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(1, 2) << endl; // -1
return 1;
}

模板类型:

#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(1, 2) << endl; // -1
return 1;
}

• 包装类对象成员函数

非模板类型:

#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(1, 2) << endl; // -1
return 1;
}

模板类型:

#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(1, 2) << endl; // -1
return 1;
}

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 ...

随机推荐

  1. iPhoneX理发指南

     iPhoneX的正面几乎都是屏幕,除了一块齐刘海(sensor housing)来放置前置摄像头和一些传感器.为了让全屏的网页在iPhoneX上有比较好的浏览效果,必须保证布局的内容不被iPhne ...

  2. 如何手写Ajax实现异步刷新

    所谓的异步刷新,就是不刷新整个网页进行更新数据. 只有通过js才能实现Ajax,进而实行异步刷新 表单提交数据和Ajax提交数据的区别:表单提交是提交的整个页面中的数据,提交数据之后会抛弃之前的页面( ...

  3. Pop Star 1.2.5

    原文链接https://www.cnblogs.com/zhouzhendong/p/Pop-Star.html 是VB写的. 年代久远,代码太丑,原码不公开. 下载链接 仅支持Windows,需要解 ...

  4. net core 获取配置文件

    IConfiguration服务是net core 默认依懒注入的一个对象 直接注入(推荐使用) 新建控制器 然后构造函数直接注入 [Route("api/[controller]" ...

  5. 20172328 2018—2019《Java软件结构与数据结构》第二周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第二周学习总结 概述 Generalization 本周学习了第三章集合概述--栈和第四章链式结构--栈.主要讨论了集合以 ...

  6. cmd应用基础 扫盲教程

    cmd是什么? 对于程序员而言,cmd命令提示符是windows操作系统下一个比较重要的工具.对于程序员而言,为了追求更高的效率而抛弃花俏的界面已然是意见很常见的行为,截止到目前的,全世界仍有大量的服 ...

  7. 【C语言程序】基因编码

    输入一个长为n=2k(k≤8)01串s,按照"ABC编码规则"进行编码,ABC编码规则是: A                      //若s串全是0 T(s)=        ...

  8. JS的string操作

    1. charAt();如果想获取字符编码,则:charCodeAt(); var stringValue ="hello world"; alert(stringValue.ch ...

  9. [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  10. JavaScript Concurrency model and Event Loop 并发模型和事件循环机制

    原文地址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop JavaScript 有一个基于 event loop 的 ...