std::bind

其实std::bind和boost::bind几乎完全一样。

普通函数使用std::bind

#include <iostream>
#include <functional>
double callableFunc (double x, double y,double z) {return x+y-z;}

int main() {
    std::function<double(double)> NewCallable1 = std::bind (callableFunc, 6,7,std::placeholders::_1);
    std::function<double(double, double)> NewCallable2 = std::bind (callableFunc,std::placeholders::_2,10,std::placeholders::_1);
    std::function<double(double, double)> NewCallable3 = std::bind (callableFunc,std::placeholders::_1,10,std::placeholders::_2);
    std::cout << NewCallable1 (8) <<std::endl;
    std::cout << NewCallable2 (9,11) <<std::endl;//callableFunc(11,10,9)
    std::cout << NewCallable3 (9,11) <<std::endl;//callableFunc(9,10,11)
    return 0;
}

bind的第一个参数是函数名,普通函数做实参时,会隐式转换成函数指针。因此std::bind (callableFunc,std::placeholders::_2,10,std::placeholders::_1)等价于std::bind(&callableFunc,std::placeholders::_2,10,std::placeholders::_1)

成员函数使用std::bind

#include <iostream>
#include <functional>
class Foo
{
public:
    void memberFunc(double d, int i, int j)
    {
        std::cout << d << std::endl;
        std::cout << i << std::endl;
        std::cout << j << std::endl;
    }
};
int main()
{
    Foo foo;
    std::function<void(int,int)> f = std::bind(&Foo::memberFunc,&foo, 1, std::placeholders::_2,std::placeholders::_1);
    f(2,3);//memberFunc(1,3,2)
    return 0;
}

bind绑定类成员函数时,第一个参数表示对象的成员函数的指针,第二个参数表示对象的地址;
必须显示的指定&Foo::memberFunc,因为编译器不会将对象的成员函数隐式转换成函数指针,所以必须在Foo::memberFunc前添加&。

lambda表达式使用std::bind

#include <iostream>
#include <functional>

int main()
{
    std::function<int(int)> f1 = [](int a) {return a; };
    std::function<int(int,int)> f2 = std::bind([](int a,int b,int c) {return a+b-c; },std::placeholders::_2,10,std::placeholders::_1);
    std::cout<<f1(1)<<std::endl;
    std::cout<<f2(9,11)<<std::endl;//11+10-9=12
    return 0;
}

std::bind使用std::ref

例:

#include <iostream>
#include <functional>

void fun(int& _a, int& _b, int _c)
{
    _a++;
    _b++;
    _c++;

    std::cout << "in    fun a:" << _a << " b:" << _b << " c:" << _c << std::endl;
}

int main()
{

    int a = 1, b = 1, c = 1;
    //a被bind传值
    //b被ref传引用
    //c被fun传值
    auto b_fun = std::bind(fun, a, std::ref(b), std::ref(c));
    b_fun();

    std::cout << "after fun a:" << a << " b:" << b << " c:" << c << std::endl;

    return 0;
}

在这里插入图片描述
分析原因:

a std::bind总是以值传递的形式传参,哪怕函数形参声明为引用;

b 如果函数形参声明为引用,且我们也非要用引用传递的形式传参,则std::bind可以使用std::ref来传引用;

c std::bind虽然使用了std::ref传递了引用,如果函数形参只支持值传递的形式,传递的仍然是值而不是引用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值