
ALINK="#ff0000">
Relational Operators
Prototypetemplate <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); DescriptionThe 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). DefinitionDefined in the standard header utility, and in the nonstandard backwardcompatibility header function.h.Requirements on typesThe 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. PreconditionsThe 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<. ComplexityExampletemplate <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); } } NotesSee alsoEquality Comparable, LessThan ComparableCopyright © 1999 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation
