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

Function Object Traits 函數對像 Traits

The header functional.hpp provides two traits class templates for functions and function objects:

頭文件 functional.hpp 為函數和函數對像提供了兩個 traits 類模板:

Type 類型 Contents 內容 Description 說明
template <typename T>
struct unary_traits
function_type The type of the function or function object itself (i.e., T).
函數或函數對像本身的類型(即 T)。
param_type The type that should be used to pass the function or function object as a parameter.
將函數或函數對像作為參數傳遞時應使用的類型。
result_type The type returned by the function or function object.
函數或函數對像返回的類型。
argument_type The type of the argument to the function or function object.
函數或函數對象的參數的類型。
template <typename T>
struct binary_traits
function_type The type of the function or function object itself (i.e., T).
函數或函數對像本身的類型(即 T)。
param_type The type that should be used to pass the function or function object as a parameter.
將函數或函數對像作為參數傳遞時應使用的類型。
result_type The type returned by the function or function object.
函數或函數對像返回的類型。
first_argument_type The type of the first argument to the function or function object.
函數或函數對象的第一個參數的類型。
second_argument_type The type of the second argument to the function or function object.
函數或函數對象的第二個參數的類型。

Usage 用法

unary_traits should be instantiated with either a function taking a single parameter, or an adaptable unary function object (i.e., a class derived from std::unary_function or one which provides the same typedefs). (See §20.3.1 in the C++ Standard.)

unary_traits 應該用一個帶單個參數的函數,或者用一個可適配的單參函數對像(即一個派生自 std::unary_function 的類或者一個提供相同 typedefs 的類)來實例化。(請見C++標準 §20.3.1)

binary_traits should be instantiated with either a function taking two parameters, or an adaptable binary function object (i.e., a class derived from std::binary_function or one which provides the same typedefs). (See §20.3.1 in the C++ Standard.)

binary_traits 應該用一個帶兩個參數的函數,或者用一個可適配的兩元函數對像(即一個派生自 std::binary_function 的類或者一個提供相同 typedefs 的類)來實例化。(請見C++標準 §20.3.1)

The most common usage of these templates is in function object adapters, thus allowing them to adapt plain functions as well as function objects. You can do this by wherever you would normally write, for example,

這些模板最常見的用法是用在函數對像適配器中,以使得適配器可以像適配函數對像一樣適配普通函數。你可以像平時所寫的那樣使用它,例如:

typename Operation::argument_type

simply writing

只要寫為:

typename boost::unary_traits<Operation>::argument_type

instead.

Additional Types Defined 其它已定義的類型

In addition to the standard result and argument typedefs, these traits templates define two additional types.

除了標準的結果和參數 typedefs, 這兩個 traits 模板還定義了另外兩個類型。

function_type

This is the type of the function or function object, and can be used in declarations such as

這是函數或函數對像本身的類型,可以像下面這樣用在聲明中:

template <class Predicate>
class unary_negate : // ...
{
// ...
private:
typename unary_traits<Predicate>::function_type pred;
};

If this typedef were not provided, it would not be possible to declare pred in a way that would allow unary_negate to be instantiated with a function type (see the C++ Standard §14.3.1 ¶3).

如果不提供這個 typedef, 那麼當允許以函數類型對 unary_negate 進行實例化時就沒有辦法聲明 pred 了(參見C++標準 §14.3.1 ¶3)。

param_type

This is a type suitable for passing the function or function object as a parameter to another function. For example,

這是將函數或函數對像作為參數傳遞給其它函數時適合使用的類型。例如:

template <class Predicate>
class unary_negate : // ...
{
public:
explicit unary_negate(typename unary_traits<Predicate>::param_type x)
:
pred(x)
{}
// ...
};

Function objects are passed by reference to const; function pointers are passed by value.

函數對像以常量引用的方式傳遞;函數指針則以值的方式傳遞。

Limitations 局限

This library uses these traits within all function object adapters, theoretically rendering ptr_fun obsolete. However, third party adapters probably won't take advantage of this mechanism, and so ptr_fun may still be required. Accordingly, this library also provides improved versions of the standard function pointer adapters.

本庫在所有函數對像適配器中都使用了這些 traits, 理論上可以不再需要 ptr_fun。但是,第三方的適配器可能還不會利用這一機制,所以可能還是需要 ptr_fun。因此,本庫也提供了 標準函數指針適配器的改良版本

These traits templates will also not work with compilers that fail to support partial specialisation of templates. With these compilers, the traits templates can only be instantiated with adaptable function objects, thus requiring ptr_fun to be used, even with the function object adapters in this library.

這些 traits 模板不能在不支持模板偏特化的編譯器上使用。對於這些編譯器,以上 traits 模板只能以可適配函數對像來進行實例化,因此雖然有了本庫中的函數對像適配器,還是需要使用 ptr_fun


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)