c++11 閉包的實現

c++11 閉包的實現

什麼是閉包

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

一個函數,帶上了一個狀態,就變成了閉包了。那什麼叫 「帶上狀態」 呢? 意思是這個閉包有屬於本身的變量,這些個變量的值是建立閉包的時候設置的,並在調用閉包的時候,能夠訪問這些變量。c++

函數是代碼,狀態是一組變量,將代碼和一組變量捆綁 (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++中現有的可調用實體的一種類型安全的包裹。經過指定它的模板參數,它能夠用統一的方式處理函數、函數對象、函數指針,並容許保存和延遲執行它們。spa

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++11

C++98中,有兩個函數bind1st和bind2nd,它們分別能夠用來綁定functor的第一個和第二個參數,它們都是隻能夠綁定一個參數,各類限制,使得bind1st和bind2nd的可用性大大下降。code

在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;
}
相關文章
相關標籤/搜索