Purpose 用途
boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions. bind does not place any requirements on the function object; in particular, it does not need the result_type, first_argument_type and second_argument_type standard typedefs.
boost::bind是标准库函数std::bind1st和std::bind2nd的一般化扩展。它支持任意的函数对象、函数、函数指针和类成员指针,它可以将特定的值绑定到任意一个参数或指定的位置。bind没有对函数对象提出任何要求,特别的,它不需要标准中定义的result_type、first_argument_type和second_argument_type等类型。
Using bind with functions and function pointers 将bind用于函数和函数指针
Given these definitions:
假定有如下定义:
int f(int a, int b) { return a + b; } int g(int a, int b, int c) { return a + b + c; }
bind(f, 1, 2) will produce a "nullary" function object that takes no arguments and returns f(1, 2). Similarly, bind(g, 1, 2, 3)() is equivalent to g(1, 2, 3).
bind(f, 1, 2)生成一个“无参数”的函数对象,这个函数对象不需要参数,返回值为f(1, 2)。类似的,bind(g, 1, 2, 3)()等价于g(1, 2, 3)。
It is possible to selectively bind only some of the arguments. bind(f, _1, 5)(x) is equivalent to f(x, 5); here _1 is a placeholder argument that means "substitute with the first input argument."
你可以有选择的只绑定某几个参数。bind(f, _1, 5)(x)等价于f(x, 5); 这里_1是一个占位符,表示“替换为第一个参数”(在这里为5)。
For comparison, here is the same operation expressed with the standard library primitives:
和标准库中提供的操作原语相比较:
std::bind2nd(std::ptr_fun(f), 5)(x);
bind covers the functionality of std::bind1st as well:
bind涵盖了std::bind1st的功能:
std::bind1st(std::ptr_fun(f), 5)(x); // f(5, x) bind(f, 5, _1)(x); // f(5, x)
bind can handle functions with more than two arguments, and its argument substitution mechanism is more general:
bind可以处理多于两个参数的函数,而且它的参数替换机制更为通用。
bind(f, _2, _1)(x, y); // f(y, x) bind(g, _1, 9, _1)(x); // g(x, 9, x) bind(g, _3, _3, _3)(x, y, z); // g(z, z, z) bind(g, _1, _1, _1)(x, y, z); // g(x, x, x)
Note that, in the last example, the function object produced by bind(g, _1, _1, _1) does not contain references to any arguments beyond the first, but it can still be used with more than one argument. Any extra arguments are silently ignored, just like the first and the second argument are ignored in the third example.
注意最后一个例子,bind(g, _1, _1, _1)生成的函数对象并不包含除第一个参数以外的其它参数,但是它仍然可以使用多个参数。任何多余的参数将会被忽略,正如在第三个例子中的第一个和第二个参数一样。
The arguments that bind takes are copied and held internally by the returned function object. For example, in the following code:
bind生成的函数对象在内部保持了一份参数的拷贝。例如下面的代码:
int i = 5; bind(f, i, _1);
a copy of the value of i is stored into the function object. boost::ref and boost::cref can be used to make the function object store a reference to an object, rather than a copy:
返回的函数对象中保存的是i的副本
An example for this:
#include < algorithm >
#include < boost / bind.hpp >
using namespace std;
void print_sum( int a, int b)
{
cout << (a + b) << endl;
}
int main()
{
int arr[ 4 ] = { 11 , 22 , 33 , 44 };
for_each(arr, arr + 4 , boost::bind(print_sum, _1, 100 ));
}
result:
111
122
133
144