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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值