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

Negators 否定器

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

頭文件 functional.hpp 提供了C++標準庫中兩個否定適配器(§20.3.5)的增強版本:

As well as the corresponding helper functions


However, the negators in this library improve on the standard versions in two ways:


Usage 用法

Usage is identical to the standard negators. For example,


bool bad(const Foo &foo) { ... }
std::vector<Foo> c;
std::find_if(c.begin(), c.end(), boost::not1(bad));

Argument Types 參數類型

The C++ Standard (§20.3.5) defines unary negate like this (binary negate is similar):


template <class Predicate>
class unary_negate
: public unary_function<typename Predicate::argument_type,bool> {
explicit unary_negate(const Predicate& pred);
bool operator()(const typename Predicate::argument_type& x) const;

Note that if the Predicate's argument_type is a reference, the type of operator()'s argument would be a reference to a reference. Currently this is illegal in C++ (but see the C++ standard core language active issues list).

注意,如果 Predicate 的 argument_type 是一個引用,則 operator() 的參數類型為引用的引用。當前在C++中這是非法的(不過請參見 C++標準核心語言活躍問題列表)。

However, if we instead defined operator() to accept Predicate's argument_type unmodified, this would be needlessly inefficient if it were a value type; the argument would be copied twice - once when calling unary_negate's operator(), and again when operator() called the adapted function.

但是,如果我們將 operator() 定義為原封不變地接受 Predicate 的 argument_type, 那麼如果參數是值類型的,就存在了不必要的效率浪費;參數將被複製兩次 - 調用 unary_negateoperator() 時一次,operator() 調用被適配函數時又一次。

So how we want to declare the argument for operator() depends on whether or not the Predicate's argument_type is a reference. If it is a reference, we want to declare it simply as argument_type; if it is a value we want to declare it as const argument_type&.

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

The Boost call_traits class template contains a param_type typedef, which uses partial specialisation to make precisely this decision. If we were to declare operator() as

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

bool operator()(typename call_traits<typename Predicate::argument_type>::param_type x) const

the desired result would be achieved - we would eliminate references to references without loss of efficiency. In fact, the actual declaration is slightly more complicated because of the use of function object traits, but the effect remains the same.

就可以得到所希望的結果 - 既消除了引用的引用,又不會損失效率。事實上,真正的聲明要稍微複雜一點,因為使用了函數對像 traits, 但是效果是一樣的。

Limitations 局限

Both the function object traits and call traits used to realise these improvements rely on partial specialisation, these improvements are only available on compilers that support that feature. With other compilers, the negators in this library behave very much like those in the Standard - ptr_fun will be required to adapt functions, and references to references will not be avoided.

函數對像 traits 和 call 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