Silicon Graphics, Inc.

Relational Operators

Category: utilities Component type: function

Prototype

template <class T> bool operator!=(const T& x, const T& y);
template <class T> bool operator>(const T& x, const T& y);
template <class T> bool operator<=(const T& x, const T& y);
template <class T> bool operator>=(const T& x, const T& y);

Description

The Equality Comparable requirements specify that it must be possible to compare objects using operator!= as well as operator==; similarly, the LessThan Comparable requirements include operator>, operator<= and operator>= as well as operator<. Logically, however, most of these operators are redundant: all of them can be defined in terms of operator== and operator<.

These four templates use operator== and operator< to define the other four relational operators. They exist purely for the sake of convenience: they make it possible to write algorithms in terms of the operators !=, >, <=, and >=, without requiring that those operators be explicitly defined for every type.

As specified in the Equality Comparable requirements, x != y is equivalent to !(x == y). As specified in the LessThan Comparable requirements, x > y is equivalent to y < x, x >= y is equivalent to !(x < y), and x <= y is equivalent to !(y < x).

Definition

Defined in function.h.

Requirements on types

The requirement for operator!= is that x == y is a valid expression for objects x and y of type T.

The requirement for operator> is that y < x is a valid expression for objects x and y of type T.

The requirement for operator<= is that y < x is a valid expression for objects x and y of type T.

The requirement for operator>= is that x < y is a valid expression for objects x and y of type T.

Preconditions

The precondition for operator!= is that x and y are in the domain of operator==.

The precondition for operator>, operator<=, and operator>= is that x and y are in the domain of operator<.

Complexity

Example

template <class T> void relations(T x, T y)
{
  if (x == y) assert(!(x != y));
  else assert(x != y);

  if (x < y) {
    assert(x <= y);
    assert(y > x);
    assert(y >= x);
  }
  else if (y < x) {
    assert(y <= x);
    assert(x < y);
    assert(x <= y);
  }
  else {
    assert(x <= y);
    assert(x >= y);
  }
}  

Notes

See also

Equality Comparable, LessThan Comparable
[Silicon Surf] [STL Home]
Copyright © 1996 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation