How to use the bind function in C++
This article introduces how to use the bind function in C++, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Bind function:
Auto newCallable = bind (callable, arg_list)
The callable manual can be:
Callable object (function object, pointer to function, reference to function, pointer to member function, or pointer to data member) that will be bound to some arguments.
Brief description of bind function:
The bind function is seen as a general-purpose function adapter that accepts a callable object callable and generates a new callable object newCallable.
It can pre-bind some parameters of the original callable object callable to a given variable (also known as parameter binding), and then generate a new callable object newCallable.
Callback functions are often used in network programming. When there is data coming from the underlying network framework, the business layer is often notified by callback functions. This allows the network layer to focus only on the sending and receiving of data, without having to care about the business.
In C language, callback functions are often implemented through function pointers. But in C++, if the callback function is a member function of a class. At this time, it is often impossible to set a member function to a callback function pointer.
Because of the member function of the class, there is an implicit parameter this. So assigning a value directly to a function pointer will definitely cause a compilation error.
Usage of bind function:
I. ordinary function
# include # include # include using namespace std::placeholders;using namespace std;void fun1 (int N1, int N2, int n3) {cout