Negators
The header
- unary_negate
- binary_negate
As well as the corresponding helper functions
- not1
- not2
However, the negators in this library improve on the standard versions in two ways:
- They use function object traits
to avoid the need for
ptr_fun when negating a function rather than an adaptable function object. - They use Boost
call traits to determine the best way to declare their arguments and pass them through to the adapted function (see below).
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
template <class Predicate>
class unary_negate
: public unary_function<typename Predicate::argument_type,bool> {
public:
explicit unary_negate(const Predicate& pred);
bool operator()(const typename Predicate::argument_type& x) const;
};
Note that if the Predicate's
However, if we instead defined
So how we want to declare the argument for
The Boost
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.
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 -
Copyright © 2000 Cadenza New Zealand Ltd. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
Revised 28 June 2000
