c++11 閉包的實現


c++11 閉包的實現

什么是閉包

閉包有很多種定義,一種說法是,閉包是帶有上下文的函數。說白了,就是有狀態的函數。更直接一些,不就是個類嗎?換了個名字而已。

一個函數,帶上了一個狀態,就變成了閉包了。那什么叫 “帶上狀態” 呢? 意思是這個閉包有屬於自己的變量,這些個變量的值是創建閉包的時候設置的,並在調用閉包的時候,可以訪問這些變量。

函數是代碼,狀態是一組變量,將代碼和一組變量捆綁 (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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM