c++ bind function explanation

1. Detailed explanation of the use of the bind function

You can think of the bind function as a generic function adapter that takes a callable object and generates a new callable object that "fits" the original object's argument list.

The general form of calling bind: auto newCallable = bind(callable,arg_list);`

Among them, newCallable itself is a callable object, and arg_list is a comma-separated parameter list corresponding to the parameters of the given callable. That is, when we call newCallable, newCallable will call callable and pass it the parameters in arg_list.

The parameters in arg_list may contain names of the form _n, where n is an integer, these parameters are "placeholders", representing the parameters of newCallable, and they occupy the "position" of the parameters passed to newCallable. The value n indicates the position of the parameter in the generated callable object: _1 is the first parameter of newCallable, _2 is the second parameter, and so on.

bind member function

#include<iostream>

#include<functional>

using namespace std;

class Plus

{

   public:

    int plus(int a,int b)

    {

        return a+b;

    }

};

int main()

{

   Plus p ;

   // Call the member function in the form of a pointer

   function<int(int,int)> func1 = std::bind(&Plus::plus,&p, placeholders::_1, placeholders::_2);

  // object form call member function

   function<int(int,int)> func2 = std::bind(&Plus::plus,p, placeholders::_1, placeholders::_2);

   cout<<func1(1,2)<<endl; //3

   cout<<func2(1,2)<<endl; //3

   return 0;

}

Corresponding output:

#include<iostream>

#include<functional>

using namespace std;

class Plus

{

   public:

    int plus(int a,int b)

    {

        cout<< __func__ << " " << a << " " << b << endl;

        return a+b;

    }

       static int plusEx ( int a , int b )  // static member function

       {

            cout<< __func__ << " " << a << " " << b << endl;

            return  a + b ;

       }

};

//bind can bind the parameters of existing functions to generate a function object

void function1 (int d)

{

    cout << __func__ << " " << d << endl;

}

//If there are multiple parameters, you can only bind part

void function2(int d1, int d2)

{

    cout<< __func__ << " " << d1 << " " << d2 << endl;

}

void fun(int& d1, int& d2, int& d3)

{

    d1 = 1 ;

    d2 = 2 ;

    d3 = 3 ;

    cout << __func__ << " " << d1 << " " << d2 << " "<< d3 <<endl;

}

int main()

{

   Plus p ;

   // Call the member function in the form of a pointer

   function<int(int,int)> func1 = std::bind(&Plus::plus, &p, placeholders::_1, placeholders::_2);

  // object form call member function

   function<int(int,int)> func2 = std::bind(&Plus::plus, p, placeholders::_1, placeholders::_2);

   cout<<func1(1,2)<<endl; //3

   cout<<func2(1,2)<<endl; //3

   // static member function

   function<int (int, int)> func3 = std::bind(&Plus::plusEx, placeholders::_1, placeholders::_2);

   cout<<func3(4,5)<<endl; //9

   

   auto f = bind ( function1 , 6 );  // bind the parameters of the function function1

   f();

  auto ff = bind ( function2 , placeholders :: _1 , 9 );  // bind the parameters of the function function1

  ff(10);

  int m1 = 4 , m2 = 5 , m3 = 6 ;

  auto fs = bind (fun, placeholders::_1, m2, ref(m3));

  fs(m1);

  //1.m1 is bound through placeholders, which is bound by reference, so the assignment in fun will affect m1

  //2.m2 is bound by parameters, it is bound by value, and the assignment in fun will not affect m2

  //3.m3 is bound by reference through ref, so the assignment in fun will affect m3

  cout << __func__ << " " << m1 << " " << m2 << " " << m3 << endl;

  return 0;

}

 

Guess you like

Origin blog.csdn.net/zuiyijiangnan/article/details/129026414