std::function

参考资料



• cplusplus.comhttp://www.cplusplus.com/reference/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.comtemplate <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;
}
时间: 2024-08-04 17:29:48

std::function的相关文章

std::function简单的理解

之前转载了一篇关于std::function,讲的太专业了,但是貌似看懂了,但是没能深入理解,今天在看案例时,突然懂得了. #define STRINGIFY(x) #x #define TRANS(__className__) { [](float t, Scene* s){ return __className__::create(t,s);}, STRINGIFY(__className__), } struct _transitions { std::function<Transitio

C++11学习笔记之三lamda表达式,std::function, std::bind

//lamda //first lamda [] {}; // second lamda []() //or no need () when paramater is null { std::cout << "second" << std::endl; }();// last add(), express will call this lamda func // 3 with return type auto kkk = []() { return 1; }()

std::function,std::bind复习

#include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std::cout << a << b << std::endl; } using namespace std::placeholders; class A { public: void fun2(int a, int b) { std::cout << a <

std::function赋值的几种方法

定义: #include <functional> std::function<void(const QString&)> myPrintFunction; 函数指针 void directPrint(const QString &msg){    qDebug()<<"direct print:"<<msg;} myPrintFunction = directPrint; lambda myPrintFunction =

std::function 使用_

1 #include <iostream> 2 #include <functional> 3 4 //函数指针写法 5 typedef int(*FuncPoint)(const int&); 6 7 8 std::function<int(const int &)> Function; 9 10 //普通函数 11 int FuncDemo(const int &value) 12 { 13 std::cout << "

c++11特性与cocos2d-x 3.0之std::bind与std::function

昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0内的Label锚点是在ccp(0.5,0.5),而一直3.0rc0是ccp(0,0). 累觉不爱.尽管cocos2d-x改变太快,兼容性一次次的暴露出不足,但是,总归是向好的方向进行.于是下载了3.0来玩玩~ cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码:

C++11 学习笔记 std::function和bind绑定器

一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法五花八门.为了统一泛化函数对象,函数指针,引用函数,成员函数的指针的各种操作,让我们可以按更统一的方式写出更加泛化的代码,C++11推出了std::function. std::function是可调用对象的包装器.它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可调用对象.通过指定它的模板参数,它可以用统一的方式处理函数,函数对象,函数指针,并允许保存和

std::bind和std::function

std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数 std::function 用于构建一个函数特别是 回调函数  ,用于替代 函数指针/*常和匿名函数一起用回调*/ 参考以下代码 #include<iostream> #include "functional" using namespace std; double calculate(double x, double y, char op) { switch (op) { c

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

1.仿函数:又叫std::function,是C++中的一个模板类 2.C语言中的函数指针: int  add(int a,int b) { return a+b; } typedef int (*func)(int,int);//给函数类型定义别名 func func1; func1=add;//给函数指针初始化 或者int (*func1)(int,int)=add; 函数指针的好处: 假设有10个函数:add,sub,mul,div,...如果采用普通的switch()  case: sw

C++11 std::function用法(c++常问问题十七)

C++11 std::function用法 直接上代码: 例子1:std::function的感觉就像是函数指针那样有木有 #include <iostream> #include <functional> #include <map> using namespace std; // 普通函数 int add(int i, int j) { return i + j; } //lambda表达式 auto mod = [](int i, int j){return i