Boost bind User Guide

Source: Internet
Author: User

Bind-boost

Header file: boost/Bind. HPP

BindIs 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 tables of BIND are: the address of the function to bind to the first parameter value and second parameter value of fun...
// the number of parameters required for fun.
bind 3 and 4 as parameters to the fun function.
All parameters are bound. now we call the function object returned by BIND:
boost: BIND (& fun, 3, 4) (); // No parameter.
3, 4

2nd usage:
bind a part of parameters to the original function Fun
boost: BIND (& fun, 3, _ 1) // whether the real parameter tables of BIND are: the address of the function to bind. It is the first parameter value of fun, then note
// 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 indicates that the new function object is called. the first parameter in the real parameter table.
// Similarly, placeholders such as _ 2 _ 3 are also used.
only fun is bound with the first parameter 3. so when you call the function object returned by BIND. required:
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 is used to bind the 1st and 2nd parameters of the real parameter table of the new function object to the fun function.
boost: BIND (& fun, _ 1, _ 2) (3, 4); // 3 will replace _ 1 placeholder, and 4 will replace _ 2 placeholder.
output 3, 4
Similarly, boost: BIND (& fun, _ 2, _ 1) (3, 4); // 3 will replace the _ 1 placeholder, 4 will replace the _ 2 placeholder.
4, 3
Similarly, boost: BIND (& fun, _ 1, _ 1) (3 ); // 3 will replace the _ 1 placeholder
3, 3

This is true for common functions.For function objects. Example:
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 Bind to 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;
...
Use STD: count_if () to calculate the number of ints values greater than 5 and <= 10.CodeUsually 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)

Boost: function object returned by BIND ()Will Save the real parameters to be bound. And always copy a copyValue MethodSave..
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...

Avoid copying this type of object.The function object to be obtained by BIND stores the reference semantics of the real parameter. You can:
Use boost ::Ref() Or boost ::Cref() For example:
BIND (& F,Ref (N)) (); // OK, n = 1 after execution

To bind an object to its member functions, for example:
A;
BIND (& A: fun,A); // The copy of object A is saved.
To avoid this copy, you can also:
BIND (& A: fun,&); // Use a pointer. You can use an object or a pointer anyway. Use a pointer to avoid copying objects.

Note:: (The following is supplemented by February 1, June 10)
The first parameter of BIND () -- bound function -- is not evaluated. For example:

typedef void (* PF) (INT);
STD: vector V; // V contains some function pointers.
STD: for_each (v. begin (), V. end (), BIND (_ 1, 5) ); // _ 1 (5 ); such a call. but this is not the case!

the correct method is to use boost: Apply template (from boost/bind/apply. HPP ).
apply is also a function object. it has the following functions:
apply 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); // It is equivalent to calling X (Y, Z);
therefore, the wrong BIND should be written:
STD: for_each (v. begin (), V. end (), BIND (apply (), _ 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.