[C++11] wrapper | bind

1. Wrapper

conceptual understanding

The function wrapper is also called an adapter.
The function in C++11 is essentially a class template and a wrapper.

The significance lies in the encapsulation and readaptation of the callable object type

Callable objects: function pointers/lambdas/functors


usage

Need to include the header file functional

Template parameter
Ret: the return type of the called function
...Args is used as a parameter pack, which contains 0 to any number of template parameters
Args...: the formal parameter of the called function


f is a function pointer, and Functor is a class. The types of the two are completely different. If
you want to declare a unified type, you need to wrap it with a wrapper to adapt to the type of a unified callable object.

function<int(int,int)>
The first int is used as the return value, and the two int parameters in the parentheses are used as the parameters of the called function.
At this time, either a function pointer can be used for initialization, or a functor can be used for initialization.


For the generated wrapper objects v1 and v2, the two types are the same
, but when calling, one calls the f function, and one calls the operator() in Functor


The wrapper is used as the second parameter of map.
The return value of the wrapper is int type. The called function has two int parameters.
You can pass f (function pointer), Functor (like function), and lambda expressions as callable objects Wrapper, initialize the wrapper

Wrappers for member functions

Member functions are divided into static member functions and non-static member functions

static member function

For static member functions, you can directly use the wrapper function
to find the plusi function through the Plus class, and initialize the wrapper

non-static member function

For ordinary member functions, there is no way to directly use the ordinary member functions of the function wrapper.
The this pointer exists in the class, so add Plus to the main function


This is not to call the function of plusd directly, but to call plusd through the Plus object


Here is not to call the plusd function directly, but to call plusd through a pointer.
If it is plus*, the anonymous object needs to take the address
, and the anonymous object is an rvalue and cannot take the address,
so use Plus* instead of anonymous objects


After adding & at the end, the problem can be solved (adding & is a rule)

2. bind

conceptual understanding

The bind function is defined in the header file and is a function template

The bind function can be seen as a general function adapter, which accepts a callable object (function pointer functor lambda object)
to generate a new callable object to adapt to the parameter list of the original object


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

The parameters in arg_list may contain names of the form _n, where n is an integer, these parameters are placeholders,
_1 represents the first parameter, _2 represents the second parameter, but by default (_1 and _2 is placed into the namespace)

Binding parameters are placed in placeholders


In the placeholders namespace, symbols such as _1 _2 _3 exist,
so if you want to use _1 to represent the first parameter, you need to access _1 in the placeholders namespace, that is, placehlders::_1

Function 1 Adjust the order of parameters (not very useful)

Under normal circumstances, pass 1 to parameter a, pass 2 to parameter b, print a=1 b=2


If you want to pass 1 to parameter b and 2 to parameter a, use bind

The first parameter of bind is
the parameter pack of the callable object bind as a placeholder, using the placeholders namespace to access the characters inside (_1 _2)

Pass the second parameter 2 in the Sub1 function to a of the Sub function
Pass the first parameter 1 in the Sub1 function to b of the Sub function

After binding, a callable object will be returned. You can choose to use auto to automatically deduce the type
and then call 1 2 again through the returned object, the result will be different


Function 2 Adjust the number of parameters

Although the func function in the Sub class seems to have only two parameters, there is also a hidden this pointer


If you use a wrapper, you need three parameters to call the func function through an anonymous object


The number of parameters in function is different from the number of parameters in func.
If you want the same number of parameters, you need to bind and adjust the number of parameters.

bind can bind a parameter to death (pass one of the parameters to the display)

insert image description here
Pass the anonymous object to the display, then the anonymous object is tied to
placehlders::_1 and placehlders::_2, indicating that the remaining two parameters
can call the object and only need to pass two parameters

Guess you like

Origin blog.csdn.net/qq_62939852/article/details/131523018