溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c++11如何實(shí)現(xiàn)閉包

發(fā)布時(shí)間:2021-06-09 09:43:51 來(lái)源:億速云 閱讀:131 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了c++11如何實(shí)現(xiàn)閉包,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

什么是閉包

一個(gè)函數(shù),帶上了一個(gè)狀態(tài),就變成了閉包了。那什么叫 “帶上狀態(tài)” 呢? 意思是這個(gè)閉包有屬于自己的變量,這些個(gè)變量的值是創(chuàng)建閉包的時(shí)候設(shè)置的,并在調(diào)用閉包的時(shí)候,可以訪問(wèn)這些變量。

函數(shù)是代碼,狀態(tài)是一組變量,將代碼和一組變量捆綁 (bind) ,就形成了閉包。

閉包的狀態(tài)捆綁,必須發(fā)生在運(yùn)行時(shí)。

仿函數(shù):重載 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++中,可調(diào)用實(shí)體主要包括:函數(shù)、函數(shù)指針、函數(shù)引用、可以隱式轉(zhuǎn)換為函數(shù)指定的對(duì)象,或者實(shí)現(xiàn)了opetator()的對(duì)象。

C++11中,新增加了一個(gè)std::function類模板,它是對(duì)C++中現(xiàn)有的可調(diào)用實(shí)體的一種類型安全的包裹。通過(guò)指定它的模板參數(shù),它可以用統(tǒng)一的方式處理函數(shù)、函數(shù)對(duì)象、函數(shù)指針,并允許保存和延遲執(zhí)行它們。

std::function對(duì)象最大的用處就是在實(shí)現(xiàn)函數(shù)回調(diào),使用者需要注意,它不能被用來(lái)檢查相等或者不相等,但是可以與NULL或者nullptr進(jìn)行比較。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <memory>
#include <functional>
#include <vector>
#include <map>

void func(void)
{// 普通全局函數(shù)
    std::cout << __FUNCTION__ << std::endl;
}

class Foo
{
public:
    static int foo_func(int a)
    {// 類中的靜態(tài)函數(shù)
        std::cout << __FUNCTION__ << "(" << a << ")->: ";
        return a;
    }
};

class Bar
{
public:
    int operator ()(int a)
    {// 仿函數(shù)
        std::cout << __FUNCTION__ << "(" << a << ")->: ";
        return a;
    }
};

void mytest()
{
    // std::function對(duì)象最大的用處就是在實(shí)現(xiàn)函數(shù)回調(diào),使用者需要注意,它不能被用來(lái)檢查相等或者不相等,但是可以與NULL或者nullptr進(jìn)行比較。

    // 綁定一個(gè)普通函數(shù)
    std::function< void(void) > f1 = func;
    f1();

    // 綁定類中的靜態(tài)函數(shù)
    std::function<int(int)> f2 = Foo::foo_func;
    std::cout << f2(11) << std::endl;

    // 綁定一個(gè)仿函數(shù)
    Bar obj;
    std::function<int(int)> f3 = obj;
    std::cout << f3(222) << std::endl;

    /*
     運(yùn)行結(jié)果:
     func
     Foo::foo_func(11)->: 11
     Bar::operator ()(222)->: 222
    */

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

std::bind

std::bind是這樣一種機(jī)制,它可以預(yù)先把指定可調(diào)用實(shí)體的某些參數(shù)綁定到已有的變量,產(chǎn)生一個(gè)新的可調(diào)用實(shí)體,這種機(jī)制在回調(diào)函數(shù)的使用過(guò)程中也頗為有用。

C++98中,有兩個(gè)函數(shù)bind1st和bind2nd,它們分別可以用來(lái)綁定functor的第一個(gè)和第二個(gè)參數(shù),它們都是只可以綁定一個(gè)參數(shù),各種限制,使得bind1st和bind2nd的可用性大大降低。

在C++11中,提供了std::bind,它綁定的參數(shù)的個(gè)數(shù)不受限制,綁定的具體哪些參數(shù)也不受限制,由用戶指定,這個(gè)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是一個(gè)占位符,代表這個(gè)位置將在函數(shù)調(diào)用時(shí),被傳入的第一個(gè)參數(shù)所替代。
    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, 調(diào)用時(shí)沒(méi)有第二個(gè)參數(shù)

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

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

通過(guò)std::bind和std::function配合使用,所有的可調(diào)用對(duì)象均有了統(tǒng)一的操作方法

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <memory>
#include <functional>
#include <vector>
#include <map>


class Test
{
public:
    int i; // 非靜態(tài)成員變量

    void func(int x, int y)
    { // 非靜態(tài)成員函數(shù)
        std::cout << x << " " << y << std::endl;
    }
};

void mytest()
{
    Test obj; // 創(chuàng)建對(duì)象
    // 綁定非靜態(tài)成員函數(shù)
    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;
    // 綁定非靜態(tài)成員變量
    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;
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“c++11如何實(shí)現(xiàn)閉包”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)
推薦閱讀:
  1. Python 閉包
  2. lua 閉包

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI