Apache C++ Standard Library Reference Guide

## Negators

Library: General utilities

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Example
- See Also
- Standards Conformance

### Local Index

No Entries

### Summary

Function adaptors and function objects used to reverse the sense of predicate function objects

### Synopsis

    #include <functional>

    namespace std {
      template <class Predicate>
      class unary_negate;

      template <class Predicate>
      unary_negate<Predicate> not1(const Predicate&);

      template <class Predicate>
      class binary_negate;

      template <class Predicate>
      binary_negate<Predicate> not2(const Predicate&);
    }

### Description

Negators `not1()` and `not2()` are functions that take predicate function objects as arguments and return predicate function objects with the opposite sense. Negators work only with function objects defined as subclasses of the classes unary_function and binary_function. `not1()` accepts and returns unary predicate function objects. `not2()` accepts and returns binary predicate function objects.

unary_negate and binary_negate are function object classes that include return types for the negators, `not1()` and `not2()`.

### Interface

    namespace std {

      template <class Predicate>
      class unary_negate
        : public unary_function
                 <typename Predicate::argument_type, bool> {

      public:
        explicit unary_negate (const Predicate&);
        bool operator() (const argument_type&) const;
      };

      template<class Predicate>
      unary_negate <Predicate> not1(const Predicate&);
    }
    template<class Predicate>
    class binary_negate
      : public binary_function
               <typename Predicate::first_argument_type,
                typename Predicate::second_argument_type, bool>
    {
    public:
      explicit binary_negate (const Predicate&);
      bool operator() (const first_argument_type&,
                       const second_argument_type&) const;
    };

    template <class Predicate>
    binary_negate<Predicate> not2 (const Predicate&);

### Example

    //
    //  negator.cpp
    //

    #include <functional>   // for unary_function
    #include <iostream>     // for boolalpha, cout, endl

    // Create a new predicate from unary_function.
    template <class Arg>
    struct is_odd : public std::unary_function<Arg, bool>
    {
        bool operator() (const Arg &arg1) const {
            return arg1 % 2 != 0;
        }
    };

    int main ()
    {
        std::less<int> less_func;

        // Use not2 on less.
        std::cout << std::boolalpha
                  << less_func (1, 4) << '\n'
                  << less_func (4, 1) << '\n'
                  << std::not2 (std::less<int>())(1, 4) << '\n'
                  << std::not2 (std::less<int>())(4, 1) << '\n';

        // Create an instance of our predicate.
        is_odd<int> odd;

        // Use not1 on our user defined predicate.
        std::cout << odd (1) << '\n'
                  << odd (4) << '\n'
                  << std::not1 (odd)(1) << '\n'
                  << std::not1 (odd)(4) << std::endl;

        return 0;
    }

    Program Output:

    true
    false
    false
    true
    true
    false
    false
    true

### See Also

Algorithms, binary_function, Function Objects, unary_function

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 20.3.5