浅谈c++11闭包的实现

目录什么是闭包仿函数:重载 operator()std::bind绑定器std::bindstd::bind和std::function配合使用

什么是闭包

一个函数,带上了一个状态,就变成了闭包了。那什么叫 “带上状态” 呢? 意思是这个闭包有属于自己的变量,这些个变量的值是创建闭包的时候设置的,并在调用闭包的时候,可以访问这些变量。

函数是代码,状态是一组变量,将代码和一组变量捆绑 (bind) ,就形成了闭包。

闭包的状态捆绑,必须发生在运行时。

仿函数:重载 operator()

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <string>#include <memory>#include <vector>#include <map>class MyFunctor{public:    MyFunctor(int temp): round(temp) {}    int operator()(int temp) {return temp + round; }private:    int round;};void mytest(){    int round = 2;    MyFunctor f(round);    std::cout << "result: " << f(1) << std::endl; // operator()(int temp)    return;}int main(){    mytest();    system("pause");    return 0;}

std::bind绑定器

在C++中,可调用实体主要包括:函数、函数指针、函数引用、可以隐式转换为函数指定的对象,或者实现了opetator()的对象。

C++11中,新增加了一个std::function类模板,它是对C++中现有的可调用实体的一种类型安全的包裹。通过指定它的模板参数,它可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟执行它们。

std::function对象最大的用处就是在实现函数回调,使用者需要注意,它不能被用来检查相等或者不相等,但是可以与NULL或者nullptr进行比较。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <string>#include <memory>#include <functional>#include <vector>#include <map>void func(void){// 普通全局函数    std::cout << __FUNCTION__ << std::endl;}class Foo{public:    static int foo_func(int a)    {// 类中的静态函数        std::cout << __FUNCTION__ << "(" << a << ")->: ";        return a;    }};class Bar{public:    int operator ()(int a)    {// 仿函数        std::cout << __FUNCTION__ << "(" << a << ")->: ";        return a;    }};void mytest(){    // std::function对象最大的用处就是在实现函数回调,使用者需要注意,它不能被用来检查相等或者不相等,但是可以与NULL或者nullptr进行比较。    // 绑定一个普通函数    std::function< void(void) > f1 = func;    f1();    // 绑定类中的静态函数    std::function<int(int)> f2 = Foo::foo_func;    std::cout << f2(11) << std::endl;    // 绑定一个仿函数    Bar obj;    std::function<int(int)> f3 = obj;    std::cout << f3(222) << std::endl;    /*     运行结果:     func     Foo::foo_func(11)->: 11     Bar::operator ()(222)->: 222    */    return;}int main(){    mytest();    system("pause");    return 0;}

std::bind

std::bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中也颇为有用。

C++98中,有两个函数bind1st和bind2nd,它们分别可以用来绑定functor的第一个和第二个参数,它们都是只可以绑定一个参数,各种限制,使得bind1st和bind2nd的可用性大大降低。

在C++11中,提供了std::bind,它绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制,由用户指定,这个bind才是真正意义上的绑定。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <string>#include <memory>#include <functional>#include <vector>#include <map>void func(int x, int y){    std::cout << x << " " << y << std::endl;}void mytest(){    std::bind(func, 1, 2)();    std::bind(func, std::placeholders::_1, 2)(1);    func(1, 2);    // std::placeholders 表示的是占位符    // std::placeholders::_1是一个占位符,代表这个位置将在函数调用时,被传入的第一个参数所替代。    std::bind(func, 2, std::placeholders::_1)(1);    std::bind(func, 2, std::placeholders::_2)(1, 2);    std::bind(func, std::placeholders::_1, std::placeholders::_2)(1, 2);    std::bind(func, std::placeholders::_3, std::placeholders::_2)(1, 2, 3);        //std::bind(func, 2, std::placeholders::_2)(1); // err, 调用时没有第二个参数    return;}int main(){    mytest();    system("pause");    return 0;}

std::bind和std::function配合使用

通过std::bind和std::function配合使用,所有的可调用对象均有了统一的操作方法

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <string>#include <memory>#include <functional>#include <vector>#include <map>class Test{public:    int i; // 非静态成员变量    void func(int x, int y)    { // 非静态成员函数        std::cout << x << " " << y << std::endl;    }};void mytest(){    Test obj; // 创建对象    // 绑定非静态成员函数    std::function<void(int, int)> f1 = std::bind(&Test::func, &obj, std::placeholders::_1, std::placeholders::_2);    f1(1, 2); // 输出: 1 2    obj.i = 10;    // 绑定非静态成员变量    std::function<int &()> f2 = std::bind(&Test::i, &obj);    f2() = 123;  // obj.i = 123;    std::cout << "obj.i: " << obj.i << std::endl;    return;}int main(){    mytest();    system("pause");    return 0;}

以上就是浅谈c++11闭包的实现的详细内容,更多关于c++11闭包的实现的资料请关注其它相关文章!

第一个青春是上帝给的;第二个的青春是靠自己努力的

浅谈c++11闭包的实现

相关文章:

你感兴趣的文章:

标签云: