Boost bind User Guide

Source: Internet
Author: User

Boost bind User Guide-Make Progress Everyday! -Blog channel-CSDN. NET

Boost bind User Guide category: c/c ++/vc 5225 people read comments (6) collect reports

Bind-boost

Header file: boost/bind. hpp

Bind is a set of overloaded function templates.
Used to bind certain parameters to a function (or function object.
The return value of bind is a function object.

Its source file is too long. You can't see it. Here we only write down its usage:

9.1 for common functions

Assume that the function fun () is as follows:
Void fun (int x, int y ){
Cout <x <"," <y <endl;
}
Now let's look at how to bind parameters to bind them.
For common functions like fun, if fun has n parameters, bind requires n + 1 parameter: Address of the original function and n parameters to be bound.

1st usage:
Bind all parameters to the original function fun
Boost: bind (& fun, 3, 4) // The real parameter table of bind is: Address of the function to be bound, bind to the first parameter value of fun, the second parameter value...
// The number of parameters required for fun.
Bind 3 and 4 as parameters to the fun function.
Because all parameters are bound, now we call the function object returned by bind:
Boost: bind (& fun, 3, 4) (); // No parameter.
The output is 3, 4.

2nd usage:
Bind a part of parameters to the original function fun
Boost: bind (& fun, 3, _ 1) // The real parameter table of bind is: the address of the function to be bound, which must be bound to the first parameter value of fun, then pay attention
// Because we do not intend to bind 2nd parameters to fun (that is, we want to specify the value of this parameter when calling the returned Functor)
// Here _ 1 is used for placeholder. Here _ 1 represents the 1st parameters of the real parameter table when the new function object is called.
// Similarly, placeholders such as _ 2 _ 3 will be used below.
Here, only fun is bound with the first parameter 3. Therefore, when calling the function object returned by bind, you need:
Boost: bind (& fun, 3, _ 1) (4); // This 4 will replace the _ 1 placeholder.
Output 3, 4
Similarly, boost: bind (& fun, _ 1, 3) (4 );
Output 4, 3

3rd usage:
Do not bind any parameters to fun
Boost: bind (& fun, _ 1, _ 2) // _ 1 _ 2 are placeholders. As mentioned above.
Therefore, it binds the 1st and 2nd parameters of the real parameter table of the new function object when calling to the fun function.
Boost: bind (& fun, _ 1, _ 2) (3, 4); // 3 will replace the _ 1 placeholder, and 4 will replace the _ 2 placeholder.
Output 3, 4
Similarly, boost: bind (& fun, _ 2, _ 1) (3, 4); // 3 will replace the _ 1 placeholder, and 4 will replace the _ 2 placeholder.
Output 4, 3
Similarly, boost: bind (& fun, _ 1, _ 1) (3); // 3 will replace the _ 1 placeholder
Outputs 3, 3

This is true for common functions. For function objects, such:
Struct Func {
Void operator () (int x ){
Cout <x <endl;
}
} F;
When binding, you may need to specify the type of the returned value:
Boost: bind <void> (f, 3) (); // specifies the type of the returned value void.
 

9.2 for non-static member functions

Assume that:
Struct {
Void func (int x, int y ){
Cout <x <"," <y <endl;
}
};
 
A;
A * pa = new A; // pointer
Boost: shared_ptr <A> ptr_a (pa); // smart pointer.
 
Now you need to bind A non-static member function like A: func.
If A: func has n parameters, bind must have n + 2 parameters: pointer to the member function fun, bound to the object of this, n parameters.
For example:
Boost: bind (& A: func, a, 3, 4) (); // output 3, 4
Boost: bind (& A: func, pa, 3, 4) (); // output 3, 4
Boost: bind (& A: func, ptr_a, 3, 4) (); // output 3, 4
You can also use placeholders such as _ 1. For example:
Boost: bind (& A: func, _ 1, 3, 4) (ptr_a); // output 3, 4

It can be seen that, no matter whether the 2nd parameters passed to bind are objects, object pointers, or smart pointers, the bind function works properly.

 

9.3 bind nesting

There is a class as follows:
Class Personal_info {
String name _;
Int age _;
Public:
Int get_age ();
String name ();
};

Vector <Personal_info> vec;
...
Sort vec now. You can use the bind function to make a comparison predicate.
Std: sort (
Vec. begin (),
Vec. end (),
Boost: bind (
Std: less <int> (),
Boost: bind (& personal_info: age, _ 1), // _ 1 placeholder is the first parameter when the comparison function is called in sort.
Boost: bind (& personal_info: age, _ 2); // _ 2 placeholder is the second parameter used to call the comparison function in sort.

9.4 function combination

Assume that:
Vector <int> ints;
...
We want to use std: count_if () to calculate the number of ints values greater than 5 and <= 10. In general code, we usually need to write a function to implement this predicate:
If (I> 5 & I <= 10 )...
Now you can use bind:
Std: count_if (
Ints. begin (), ints. end (),
Boost: bind (
Std: logical_and <bool> (),
Boost: bind (std: greater <int> (), _ 1, 5 ),
Boost: bind (std: less_equal <int> (), _ 1, 10 )));

 

9.5 bind to member variables

Include:
Map <int, string> my_map;
My_map [0] = "Boost"; my_map [1] = "Bind ";
Now we need to output the second members of all elements. That is, to output these strings. The print function is as follows:
Void print_string (const string & s ){
Std: cout <s <'/N ';
}
You can:
For_each (
My_map.begin (),
My_map.end (),
Boost: bind (
& Print_string,
Boost: bind (& std: map <int, std: string >:: value_type: second, _ 1)
)
);

Khan... I don't know the source code of bind. I don't know how to implement these functions. I have to wait for <boost source code analysis>.

Note:
(The following is supplemented by June 3)

The function object returned by boost: bind () will save the real parameters to be bound, and will always copy a copy to save as a value ..
This mainly takes into account the life cycle of the bound real parameters.
But this is not always what we expect. For example, sometimes we want it to save pointers or references:

Function:
Void f (int & x) {++ x ;}
Then:
Int n = 0;
Bind (& f, n) (); // We want n = 1. but actually this is not...

To avoid copying such an object, save the reference semantics of the real parameter for the function object to be obtained by bind. You can:
Use boost: ref () or boost: cref () such
Bind (& f, ref (n) (); // OK, after execution, n = 1

To bind an object to its member functions, for example:
A;
Bind (& A: fun, a); // stores the copy of object.
To avoid this copy, you can also:
Bind (& A: fun, & a); // use A pointer. You can use an object or a pointer. Instead, use A pointer to avoid copying objects.

Note: (Add the following to April 1, June 10)
The first parameter of bind () -- bound function -- is not evaluated. For example:

Typedef void (* pf) (int );
Std: vector <pf> v; // There are some function pointers in v.
Std: for_each (v. begin (), v. end (), bind (_ 1, 5); // you can call this method to implement _ 1 (5. but this is not the case!

The correct method is to use the boost: apply template (from boost/bind/apply. hpp ).
Apply is also a function object. Its functions are as follows:
Apply <void> a; // The template parameter is the return value type of the function object.
A (x); // call x ();
A (x, y); // call x (y );
A (x, y, z); // call x (y, z );
Therefore, the wrong bind should be written as follows:
Std: for_each (v. begin (), v. end (), bind (apply <void> (), _ 1, 5 ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.