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

Function Pointer Adapters 函數指針適配器

The header functional.hpp provides enhanced versions of both the function pointer adapters from the C++ Standard Library (§20.3.7):

頭文件 functional.hpp 提供了對C++標準庫的兩個函數指針適配器(§20.3.7)的增強版本:

As well as the corresponding helper function template:

以及相應的輔助函數模板:

However, you should not need to use the adapters in conjunction with the adapters in this library due to our use of function object traits. You will however need to use them if your implementation fails to work properly with our traits classes (due to lack if partial specialisation), or if you wish to use a function object adapter from a third party.

不過,你不需要將這些適配器與本庫中的其它適配器一起使用,因為我們使用了 函數對像 traits. 如果你的實現不能讓我們的 traits 類正常工作(如缺乏偏特化的支持), 或者如果你想使用第三方的函數對像適配器,這時你才需要用它們。

Usage 用法

If you need to use these adapters, usage is identical to the standard function pointer adapters. For example,

如果你要使用這些適配器,它們的用法與標準的函數指針適配器是一樣的。例如:

bool bad(std::string foo) { ... }
...
std::vector<std::string> c;
...
std::vector<std::string>::iterator it
= std::find_if(c.begin(), c.end(), std::not1(boost::ptr_fun(bad)));

Note however that this library contains enhanced negators that support function object traits, so the line above could equally be written

注意,本庫包含的增強 否定器 支持函數對像 traits, 所以上面一行相當於:

std::vector<std::string>::iterator it
= std::find_if(c.begin(), c.end(), boost::not1(bad));

Argument Types 參數類型

The standard defines pointer_to_unary_function like this (§20.3.8 ¶2):

標準的 pointer_to_unary_function 定義如下(§20.3.8 ¶2):

template <class Arg, class Result>
class pointer_to_unary_function : public unary_function<Arg, Result> {
public:
explicit pointer_to_unary_function(Result (* f)(Arg));
Result operator()(Arg x) const;
};

Note that the argument to operator() is exactly the same type as the argument to the wrapped function. If this is a value type, the argument will be passed by value and copied twice. pointer_to_binary_function has a similar problem.

注意,operator() 的參數與被包裝函數的參數是完全相同的類型。如果它是一個值類型,則參數將被以值方式傳遞,並且複製兩次。pointer_to_binary_function 也有同樣的問題。

However, if we were to try and eliminate this inefficiency by instead declaring the argument as const Arg&, then if Arg were a reference type, we would have a reference to a reference, which is currently illegal (but see C++ core language issue number 106)

但是,如果我們試圖消除這一低率的操作,將參數的聲明改為 const Arg&, 那麼如果 Arg 是一個引用類型,則我們就遇到了引用的引用,這在當前是非法的(不過請參見 C++核心語言問題 106)

So the way in which we want to declare the argument for operator() depends on whether or not the wrapped function's argument is a reference. If it is a reference, we want to declare it simply as Arg; if it is a value we want to declare it as const Arg&.

所以,我們如何聲明 operator() 的參數,取決於被包裝函數的參數是否為引用。如果它是一個引用,則我們只要將它聲明為 Arg; 如果它是一個值,則我們應該聲明它為 const Arg&.

The Boost call_traits class template contains a param_type typedef, which uses partial specialisation to make precisely this decision. By declaring the operator() as

Boost call_traits 類模板包含了一個 param_type typedef, 它使用了偏特化來作出恰當的決定。通過將 operator() 聲明為

Result operator()(typename call_traits<Arg>::param_type x) const

we achieve the desired result - we improve efficiency without generating references to references.

我們就得到了所希望的結果 - 既提高了效率,又不會產生引用的引用。

Limitations 局限

The call traits template used to realise this improvement relies on partial specialisation, so this improvement is only available on compilers that support that feature. With other compilers, the argument passed to the function will always be passed by reference, thus generating the possibility of references to references.

call traits 模板依賴於偏特化,所以這一改進只能在支持這一特性的編譯器上使用。對於其它編譯器,傳遞給函數的參數總是以引用的方式進行傳遞,因此有可能產生引用的引用。


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 http://www.boost.org/LICENSE_1_0.txt)