boost.png (6897 bytes) Home Libraries People FAQ More

Improved Function Object Adapters 改良的函數對像適配器

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 節)提供了增強。主要由以下兩個變化提供了改良:

  1. We use the Boost call_traits templates to avoid the problem of references to references, and to improve the efficiency of parameter passing.
    我們使用 Boost call_traits 模板來避免 引用的引用 的問題,並提高了 參數傳遞 的效率。
  2. We use two function object traits class templates to avoid the need for ptr_fun with the adapters in this library.
    我們使用兩個 函數對像 traits 類模板來避免本庫中對帶適配器的 ptr_fun 的需要。

Contents 目錄

The header contains the following function and class templates:


Function object traits
函數對像 traits
Used to determine the types of function objects' and functions' arguments. Eliminate the necessity for ptr_fun.
用於判定函數對像和函數參數的類型。消除了對 ptr_fun 的需要。
Negators 否定器 unary_negate
Based on section 20.3.5 of the standard.
基於標準的第 20.3.5 節。
Binders 綁定器 binder1st
Based on section 20.3.6 of the standard.
基於標準的第 20.3.6 節。
Adapters for pointers to functions
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
Based on section 20.3.8 of the standard.
基於標準的第 20.3.8 節。

Usage 用法

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
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 文檔 中的註解。

Compiler Compatibility 編譯器兼容性

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:
  • The call_traits mechanism is unable to prevent references to references, and so the adapters in this library will be usable in fewer situations.
    call_traits 機制不能防止引用的引用,所以本庫中的適配器只能用於更少的情況。
  • The function_traits mechanism is unable to determine the argument and result types of functions, therefore ptr_fun continues to be required to adapt functions.
    function_traits 機制不能判定函數的參數和結果類型,因此仍然需要用 ptr_fun 來適配函數。

Future Directions 未來的方向

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:


  1. Several Boost members are working on expression template libraries. These will allow a more natural syntax for combining and adapting functions. As this is a new technology, it may be some time before it has matured and is widely supported by major compilers but shows great promise. In the meantime, the functional.hpp library fills the gap.
    多 個 Boost 成員正為表達式模板庫而工作。它們將允許使用更為自然的語法來組合和適配函數。由於這是一種新的技術,在它成熟並獲得主流編譯器的廣泛支持之前可能還需要 一些時間,但是已經顯示出非常大的希望。在此期間,functional.hpp 庫填補了這一空白。
  2. The Standard Committee has recognised the problem of references to references occurring during template instantiation and has moved to fix the standard (see the C++ standard core language active issues list).
    標準委員會已經認識到在模板實例化中發生的"引用的引用"這一問題,並且已著手於修正標準(請見 C++ 標準核心語言活躍問題列表)。

Author 作者

Mark Rodgers

Acknowledgements 鳴謝

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.

Valid HTML 4.01 Transitional

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