std::bind和std::function

  std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数,特别是在多线程的程序中,经常用它将函数进行包装,然后打包发送给工作线程,让工作线程去执行我们的任务。

  std::function 用于构建一个函数特别是 回调函数  ,用于替代 函数指针/*常和匿名函数一起用回调*/

参考以下代码:

#include<iostream>


#include "functional"

using namespace std;

double calculate(double x, double y, char op)
{
    switch (op)
    {

    case  '+':
    {
                 return x + y;
    }break;

    case  '-':
    {
                 return x - y;
    }break;
    }
    return 0;
}


int main()
{
    
    
    //bind
    auto plus = std::bind(calculate, std::placeholders::_1, std::placeholders::_2, '+');/*calculate 参数1 参数2*/
    auto sub = std::bind(calculate, std::placeholders::_2, std::placeholders::_1, '-');/*calculate 参数2 参数1*/
    cout << plus(5, 2) << endl;
    cout << sub(5, 2) << endl;/*调用时候 5为 calculate的第二个参数*/


    //function
    typedef    std::function<double(double , double , char)> func;
    func fun = calculate;
    cout << fun(1, 2, '+') << endl;;

    //function ptr
     double (*p)(double, double, char) =calculate;
     cout << p(1, 1, '+') << endl;;


    //function + bind
    std::function<double (double , double )> plus_2;
    plus_2 = plus;
    cout << plus_2(2, 3) << endl;;

    system("pause");
    return 0;

}

  bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调 用实体,这种机制在回调函数的使用过程中也颇为有用。其实最早在C++98的时候,就已经有了std::bind1st和std::bind2nd分别用来绑定functor的两个参数,具体代码就不演示了,查查资料就知道了。这个特性在当时并没有引起太多的重视,可以说是食之无味。
  C++11中提供了std::bind,可以说是一种飞跃的提升,bind本身是一种延迟计算的思想,它本身可以绑定普通函数、全局函数、静态函数、类静态函数甚至是类成员函数。

#include <iostream>
#include <functional>
using namespace std;

int TestFunc(int a, char c, float f)
{
    cout << a << endl;
    cout << c << endl;
    cout << f << endl;

    return a;
}

int main()
{
    auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
    bindFunc1(10);

    cout << "=================================
";

    auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
    bindFunc2('B', 10);

    cout << "=================================
";

    auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
    bindFunc3(100.1, 30, 'C');

    return 0;
}

  从上面的代码可以看到,bind能够在绑定时候就同时绑定一部分参数,未提供的参数则使用占位符表示,然后在运行时传入实际的参数值。

  PS:绑定的参数将会以值传递的方式传递给具体函数,占位符将会以引用传递。

  众所周知,静态成员函数其实可以看做是全局函数,而非静态成员函数则需要传递this指针作为第一个参数,所以std::bind能很容易地绑定成员函数。

  bind最终将会生成一个可调用对象,这个对象可以直接赋值给std::function对象,而std::bind绑定的可调用对象可以是Lambda表达式或者类成员函数等可调用对象。它能随意绑定任何函数,将所有的函数都能统一到std::function。

原文地址:https://www.cnblogs.com/renyuan/p/6224967.html