Книга: Standard Template Library Programmer
Functions
Разделы на этой странице:
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 the standard header utility, and in the nonstandard backward-compatibility header 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<.
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);
}
}
See also
Equality Comparable, LessThan Comparable
- CHAPTER 4 Functions and Libraries in mikroC
- Basic Functions
- Using Functions in Shell Scripts
- 4.1 mikroC Functions
- 4.2 mikroC Built-in Functions
- 4.3 mikroC Library Functions
- 4.1.2 Passing Arrays to Functions
- 4.1.3 Passing Variables by Reference to Functions
- 7.2 mikroC Language SD Card Library Functions
- 8.6 mikroC Language USB Bus Library Functions
- 9.10 mikroC CAN Functions
- 12.3.2 Mapping Generic Functions to Driver Functions