class template
<functional>

std::unary_negate

template <class Predicate> class unary_negate;
Negate unary function object class
Unary function object class whose call returns the opposite of another unary function, passed to its constructor.

Object of type unary_negate are generally constructed using function not1.

The class is defined with the same behavior as:

1
2
3
4
5
6
7
8
9
template <class Predicate> class unary_negate
  : public unary_function <typename Predicate::argument_type,bool>
{
protected:
  Predicate fn_;
public:
  explicit unary_negate (const Predicate& pred) : fn_ (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x);}
};
1
2
3
4
5
6
7
8
9
10
template <class Predicate> class unary_negate
{
protected:
  Predicate fn_;
public:
  explicit unary_negate (const Predicate& pred) : fn_ (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x);}
  typedef typename Predicate::argument_type argument_type;
  typedef bool result_type;
};

Template parameters

Predicate
A unary function object class, with member argument_type defined.

Member types

member typedefinitionnotes
argument_typeTType of the argument in member operator()
result_typeTType returned by member operator()

Member functions

constructor
Constructs an object whose functional call returns the opposite as the object passed as its argument.
operator()
Member function returning the opposite of the function object with which the object was constructed.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// unary_negate example
#include <iostream>     // std::cout
#include <functional>   // std::unary_negate
#include <algorithm>    // std::count_if

struct IsOdd_class {
  bool operator() (const int& x) const {return x%2==1;}
  typedef int argument_type;
} IsOdd_object;

int main () {
  std::unary_negate<IsOdd_class> IsEven_object (IsOdd_object);
  int values[] = {1,2,3,4,5};
  int cx;
  cx = std::count_if ( values, values+5, IsEven_object );
  std::cout << "There are " << cx << " elements with even values.\n";
  return 0;
}

Output:

There are 2 elements with even values.


See also