std::function 的使用说明
转自:
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 的使用说明的更多相关文章
- C++11中的std::function
看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...
- C++11之std::function和std::bind
std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...
- typedef 函数指针 数组 std::function
1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA, *PDATA; //D ...
- callable object与新增的function相关 C++11中万能的可调用类型声明std::function<...>
在c++11中,一个callable object(可调用对象)可以是函数指针.lambda表达式.重载()的某类对象.bind包裹的某对象等等,有时需要统一管理一些这几类对象,新增的function ...
- std::bind和std::function
std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数,特别是在多线程的程序中,经常用它将函数进行包装,然后打包发送给工作线程,让工作线程去执行我们的任务. ...
- std::function,std::bind
std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...
- C++11 std::function用法
转自 http://www.hankcs.com/program/cpp/c11-std-function-usage.html function可以将普通函数,lambda表达式和函数对象类统一起来 ...
- 【转】C++11中的std::function
原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...
- std::function,std::bind复习
#include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...
随机推荐
- 阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试
阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试 结论: 优先选择CentOS/Ubuntu,可选AliyunLinux(CentOS修改版) ...
- python之re正则简单够用
0. 1.参考 Python正则表达式指南 https://docs.python.org/2/library/re.html https://docs.python.org/2/howto/rege ...
- Linux下Apache配置HTTPS功能
Apache配置HTTPS功能 转 https://www.cnblogs.com/liaojiafa/p/6028816.html 一.yum 安装openssl和openssl-devel,ht ...
- 【java】-- java并发包总结
1.同步容器类 1.1.Vector与ArrayList异同 1.Arraylist和Vector都是采用数组方式存储数据,都允许直接序号索引元素,所以查找速度快,但是插入数据等操作涉及到数组元素移动 ...
- vscode断点调试本地客户端文件
一.安装chrome,安装vscode,打开vscode编辑器,安装插件Debugger for Chrome 二.新建文件 1.目录结构 . ├── index.html ├── index.js ...
- Python 中的单例模式
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- django缓存、信号、序列化
本篇导航: Django的缓存机制 Django的信号 Django的序列化 一.Django的缓存机制 1.缓存介绍 1)缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增, ...
- vue_源码 原理 剖析
相关基础知识点 // 可以让 任意函数/方法 成功临时指定成对象的方法进行调用 - call/apply // 1. 根据伪数组生成 真数组 const lis = document.getEleme ...
- JavaScript学习day2 (基本语法上)
知识点 JavaScript 的变量 数据类型 运算符 JavaScript 的动态类型 变量:(变量的命名规则和其他语言类似) 由数字,字母,下划线组成,区分大小写 以字母开头 变量名不能有空格 不 ...
- django项目同一用户不能同时登陆
1.session认证 ..... login(request, user) #登录成功 # 登录之后获取获取最新的session_key session_key = request.session. ...