C++11系列三—std::function和std::bind

旧标准的缺陷

学习新标准的语法之前,先来聊聊旧标准存在的缺陷,这样就可以理解为什么要在C++11中存在std::funtion和std::bind了,这就是所谓——知其然,并知其所以然

繁杂的调用对象

C++中可以调用的对象有很多,包括但不限于以下:

  1. 函数指针
  2. 仿函数(重载了"( )" 运算符)
  3. 可被转换为函数指针的类对象
  4. 类成员函数

问题浮出水面

这些调用对象在使用的时候具有比较统一的时候,但是定义的方式却是五花八门。因此,C++11中为了改善这个问题,提供了std::function和std::bind来解决

std::function

std::function是可调用对象的包装器本质上是一个类模板可容纳除了类成员(函数)指针之外的所有可调用对象

小试牛刀

#include<iostream>
#include<functional>

class A
{
    std::function<void()> callback_;
public:
    A(const std::function<void()>& f) : callback_(f){}
    void notify(void){
        callback_(); //调用这个回调对象
    }
};
class Foo{
public:
    void operator()(void){
        std::cout << __FUNCTION__ << std::endl;
    }
};
int main(void)
{
    Foo foo;
    A aa(foo);
    aa.notify();  //调用这个回调
    
    return 0;
}    

//输出
//operator()

std::function因为有着保存函数并可以延迟执行的特性,因此非常适合作为回调函数来使用

std::bind

std::bind用来将可调用对象与其参数一起进行绑定,绑定后的结果可以使用std::function进行保存,并延迟调用

作用

  1. 将可调用对象与其参数一起绑定成一个仿函数
  2. 将N元可调用对象转换成一元或N-1元可调用对象,即只绑定部分参数

占位符

std::bind可以将函数及其参数进行绑定,那么参数的位置与传入的位置相关,因此有了占位符的概念,例如std::placeholders::_1是第一个参数的占位符,当函数传入第一个参数的时候,就会把其进行替换,同理std::palceholders::_2是第二个占位符。

#include<iostream>
#include<functional>

void output(int x, int y){
    std::cout << x << " " << y << std::endl;
}

int main(){
    std::bind(output, 1, 2)(); //输出:1 2
    std::bind(output, std::placeholders::_1)(1); //输出:1 
    std::bind(output, 2, std::placeholders::_2)(1,2); //输出:2 2
    std::bind(output, std::placeholders::_2, std::placeholders::_1)(1, 2); //输出:2 1
}

高级用法

可以使用std::bind进行多个函数的组合,例如找出集合中大于5小于10的元素个数:

#include<iostream>
#include<functional>

using std::placeholders::_1;

auto f = std::bind(std::logical_and<bool>(), std::bind(std::greater<int>(), _1, 5),std::bind(std::less_equal<int>(), _1, 10)); //将两个函数参数绑定并进行组合

int count = std::count_if(coll.begin(), coll.end(), f); //使用

双剑合璧

std::bind和std::function配合使用非常的强大

#include<iostream>
#include<funcational>

class A{
public:
    int i_ = 0;
    void output(int x, int y){
        std:cout << x << " " << y << std::endl;
    }  
};
int main(void){
    A a;
    std::function<void(int, int)> fr = std::bind(&A::output, &a, std::placeholders::_1,std::palceholders::_2);   //注意
    fr(1, 2);						//输出:1 2
    
    std::function<int&(void)> fr_i = std::bind(&A::i, &a);
    fr_i() = 123;
    std::cout << a.i_ << std::endl; //输出:123
}

可以看到上面std::bind在绑定类的成员函数的时候,需要把&a也传进去,这是因为成员函数编译器在使用的时候会自动传入一个this指针,所以我们绑定的时候需要额外传一个对象的地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星光 不服赶路人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值