![]() |
Home | Libraries | People | FAQ | More |
The header functional.hpp provides enhancements to the function object adapters specified in the C++ Standard Library (sections 20.3.5, through to 20.3.8). The enhancements are principally possible due to two changes:
頭文件 functional.hpp 為C++標準庫中的函數對像適配器(第 20.3.5 至 20.3.8 節)提供了增強。主要由以下兩個變化提供了改良:
The header contains the following function and class templates:
頭文件包含了以下函數和類模板:
| Function
object traits 函數對像 traits |
unary_traits binary_traits |
Used to determine the types of
function objects' and functions' arguments. Eliminate the necessity for
ptr_fun. 用於判定函數對像和函數參數的類型。消除了對 ptr_fun 的需要。 |
|---|---|---|
| Negators 否定器 | unary_negate binary_negate not1 not2 |
Based on section 20.3.5 of the
standard. 基於標準的第 20.3.5 節。 |
| Binders 綁定器 | binder1st binder2nd bind1st bind2nd |
Based on section 20.3.6 of the
standard. 基於標準的第 20.3.6 節。 |
| Adapters
for pointers to functions 用於函數指針的適配器 |
pointer_to_unary_function pointer_to_binary_function ptr_fun |
Based on section 20.3.7 of the
standard. Not required for use with this library since the binders and
negators can adapt functions, but may be needed with third party
adapters. 基於標準的第 20.3.7 節。使用本庫時可以不需要它,因為綁定器和否定器就可以用於適配函數,但可能需要用於第三方的適配器。 |
| Adapters
for pointers to member functions 用於成員函數指針的適配器 |
mem_fun_t mem_fun1_t const_mem_fun_t const_mem_fun1_t mem_fun_ref_t mem_fun1_ref_t const_mem_fun_ref_t const_mem_fun1_ref_t mem_fun mem_fun_ref |
Based on section 20.3.8 of the
standard. 基於標準的第 20.3.8 節。 |
Using these adapters should be pretty much the same as using the standard function object adapters; the only differences are that you need to write boost:: instead of std::, and that you will get fewer headaches.
使用這些適配器和使用標準的函數對像適配器幾乎一樣;唯一的區別在於,你需要寫 boost:: 而不是 std::, 而且你會更少遇到頭痛的事。
For example, suppose you had a Person class that contained a set_name function:
例如,假設你有一個 Person 類,它有一個 set_name 函數:
class Person
{
public:
void set_name(const std::string &name);
// ...
};
You could rename a bunch of people in a collection, c, by writing
你可以對集合 c 中的一群人進行改名,這樣寫
std::for_each(c.begin(), c.end(),
boost::bind2nd(boost::mem_fun_ref(&Person::set_name), "Fred"));
If the standard adapters had been used instead then this code would normally fail to compile, because set_name takes a reference argument. Refer to the comments in the binder documentation to explain why this is so.
如果使用的是標準的適配器,則這段代碼不能正常編譯,因為 set_name 接受的是一個引用參數。對此的解釋請參見 binder 文檔 中的註解。
The header and test program have been compiled with the following compilers:
相關頭文件和 測試程序 已經在以下編譯器通過編譯:
| Compiler 編譯器 | Comments 說明 |
|---|---|
| Borland C++Builder 4 Update 2 | No known issues. 沒有已知的問題。 |
| Borland C++ 5.5 | No known issues. 沒有已知的問題。 |
| g++ 2.95.2 | No known issues. 沒有已知的問題。 |
| Microsoft Visual C++ Service Pack 3 | Compiler lacks partial
specialisation, so this library offers little more than is provided by
the standard adapters:
編譯器缺乏偏特化,因此本庫所提供的與標準適配器的一樣:
|
This library's primary focus is to solve the problem of references to references while maintaining as much compatibility as possible with the standard library. This allows you to use the techniques you read about in books and magazines with many of today's compilers.
本庫的首要目標是解決引用的引用這一問題,同時盡可能保持與標準庫的兼容性。這樣可以允許你在目前多數編譯器上使用你在書本或雜誌上所 看到的有關技巧。
In the longer term, even better solutions are likely:
長遠來說,更好的解決辦法很可能是:
Thanks to John Maddock for suggesting the mechanism that allowed the function objects traits to work correctly. Jens Maurer provided invaluable feedback during the formal review process.
Revised 02 December, 2006
Copyright c 2000 Cadenza New Zealand Ltd.
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)