1. 程式人生 > >c++11 std::bind std::function

c++11 std::bind std::function

std::function是可呼叫物件的包裝器,它最重要的功能是實現延時呼叫
我們給std::function填入合適的函式簽名(即一個函式型別,只需要包括返回值和引數表)之後,它就變成了一個可以容納所有這一類呼叫方式的“函式包裝器”。
格式 std::function<返回值(引數表)>

例:
#include "stdafx.h"
#include<iostream>// std::cout
#include<functional>// std::function
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;
    }
};


int main()
{
    // 繫結普通函式
    std::function<void(void)> fr1 = func;
    fr1();


    // 繫結類的靜態成員函式
    std::function<int(int)> fr2 = Foo::foo_func;
    std::cout << fr2(100) << std::endl;


    // 繫結仿函式
    Bar bar;
    fr2 = bar;
    std::cout << fr2(200) << std::endl;


    return 0;
}

由上邊程式碼定義std::function<int(int)> fr2,那麼fr2就可以代表返回值和引數表相同的一類函式。可以看出fr2儲存了指代的函式,可以在之後的程式過程中呼叫。這種用法在實際程式設計中是很常見的。
#include <iostream>  
#include <functional>  
using namespace std;  
class A  
{  
public:  
    A(const std::function<void()>& f){  
        :callback_(f){}  
      
    void notify(void){  
        callback_();  
    }  
private:  
    std::function<void()> callback_;  
};  
  
class Foo  
{  
public:  
    void operator()(void){  
        cout << __FUNCTION__<< endl;  
    }  
};  
  
int main(){  
    Foo foo;  
    A aa(foo);  
    aa.notify();  
      
    return 0;  
}  
從上面的例子看,std::function可以取代函式指標的作用。因為它可以儲存函式延遲執行,所以比較適合作為回撥函式
,也可以把它看做類似於C#中特殊的委託(只有一個成員的委託)。
#include <iostream>  
#include <functional>  
using namespace std;  
void call_when_even(int x, const std::function<void(int)>& f){  
    if(!(x & 1)){  
        f(x);  
    }  
}  
void output(int x){  
    cout << x <<" ";  
}  
  
int main(void){  
    for(int i=0;i<10;i++){      
        call_when_even(i, output);  
    }  
    cout<<endl;  
  
    return 0;  
}

std::function還可以作為函式入參,這樣可以在函式外部控制函式的內部行為了,讓我們的函式變得更加靈活。

C++11中提供了std::bind,bind本身是一種延遲計算的思想,它本身可以繫結普通函式、全域性函式、靜態函式、類靜態函式甚至是類成員函式。
std::bind用來將可呼叫物件與其引數一起進行繫結。繫結後可以使用std::function進行儲存,並延遲到我們需要的時候呼叫
(1) 將可呼叫物件與其引數繫結成一個仿函式;
(2) 可繫結部分引數。
在繫結部分引數的時候,其他引數可以直接傳入實參,通過使用std::placeholders來決定空位引數將會是呼叫發生時的第幾個引數。請看最後一個例子
例:
#include "stdafx.h"
#include<iostream>// std::cout
#include<functional>// std::function
class A
{
public:
    int i_ = 0; // C++11允許非靜態(non-static)資料成員在其宣告處(在其所屬類內部)進行初始化
    void output(int x, int y)
    {
        std::cout << x << "" << y << std::endl;
    }
};
int main()
{
    A a;
    // 繫結成員函式,儲存為仿函式  fr前面的一坨包裝器可以用auto表示

    std::function<void(int, int)> fr = std::bind(&A::output, &a, std::placeholders::_1, std::placeholders::_2);
    // 呼叫成員函式
    fr(1, 2);
    // 繫結成員變數
    std::function<int&(void)> fr2 = std::bind(&A::i_, &a);
    fr2() = 100;// 對成員變數進行賦值
    std::cout << a.i_ << std::endl;
    return 0;

}

#include <iostream>
using namespace std;
class A
{
public:
    void fun_3(int k,int m)
    {
        cout<<k<<" "<<m<<endl;
    }
};

void fun(int x,int y,int z)
{
    cout<<x<<"  "<<y<<"  "<<z<<endl;
}

void fun_2(int &a,int &b)
{
    a++;
    b++;
    cout<<a<<"  "<<b<<endl;
}

int main(int argc, const char * argv[])
{
    auto f1 = std::bind(fun,1,2,3); //表示繫結函式 fun 的第一,二,三個引數值為: 1 2 3
    f1(); //print:1  2  3

    auto f2 = std::bind(fun, placeholders::_1,placeholders::_2,3);
    //表示繫結函式 fun 的第三個引數為 3,而fun 的第一,二個引數分別有呼叫 f2 的第一,二個引數指定
    f2(1,2);//print:1  2  3

   auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3);
    //表示繫結函式 fun 的第三個引數為 3,而fun 的第一,二個引數分別有呼叫 f3 的第二,一個引數指定
    //注意: f2  和  f3 的區別。
    f3(1,2);//print:2  1  3
    int n = 2;
    int m = 3;

    auto f4 = std::bind(fun_2, n,placeholders::_1);
    f4(m); //print:3  4

    cout<<m<<endl;//print:4  說明:bind對於不事先繫結的引數,通過std::placeholders傳遞的引數是通過引用傳遞的
    cout<<n<<endl;//print:2  說明:bind對於預先繫結的函式引數是通過值傳遞的
    A a;
    auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2);
    f5(10,20);//print:10 20

    std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2);
    fc(10,20);//print:10 20
    return 0;

}

使用中的:

typedef std::function<

void (const VideoConfig &config,
unsigned char *data, size_t size,
long long startTime, long long stopTime)

> VideoProc;

VideoProc   callback;

videoConfig.callback = std::bind(&DShowInput::OnVideoData, this,
placeholders::_1, placeholders::_2,
placeholders::_3, placeholders::_4,placeholders::_5);

videoConfig.callback(videoConfig, data, size, startTime,
stopTime);