参考资料


• 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. TF常用知识

    命名空间及变量共享 # coding=utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt; ...

  2. mac中安装lua5.1.5

    lua有一个工具lua-releng( https://github.com/openresty/openresty-devel-utils/blob/master/lua-releng) 用来检测代 ...

  3. .NET Core 2.1 IIS 部署 出现500.19 错误

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 最后发现是由于项目从 .NET Core 1.0 升级到 .NET Co ...

  4. Hbase:原理和设计

    转载自:http://www.sysdb.cn/index.php/2016/01/10/hbase_principle/ ,感谢原作者. 简介 HBase —— Hadoop Database的简称 ...

  5. 匿名函数gc分析

    测试一:使用member function创建action会产生gc,不管该函数是否访问外部变量: private System.Action memberAct = null; // gc 112B ...

  6. windows下IDEA的terminal配置bash命令

    使用git-bash.exe会单独打开一个窗口,而我们希望是在终端内置的命令行.这里我使用bash.exe 在IDEA中,打开settings,设置相应的bash路径 settings–>Too ...

  7. [转java发送http的get、post请求]

    Http请求类 package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io. ...

  8. mysql 字符串 拼接 截取 替换

    一. 字符串拼接 concat('asdf',str); 说明: 拼接asdf 和 str 二. 字符串截取 从左开始截取字符串 left(str, length) 说明:) as abstract ...

  9. java数值比较

    import org.junit.Test; public class NumberCompare { /** * 数值比较,以Float为例 */ @Test public void testNam ...

  10. window alias给cmd命令起别名

    场景: Linux的alias命令是个非常实用的工具,任何命令通过alias可以精简到很短,比如:alias l='ls -l' Windows也有alias类似的命令,就是:doskey,开启方法也 ...