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 <iostream>
#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