| 类别:实用工具 | 组件类型:函数 |
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);
这四个模板使用operator==和operator<来定义另外四个关系运算符。它们的存在纯粹是为了方便:它们使编写算法成为可能,算法涉及运算符!=, >, <=,和>=, 而不要求为每种类型显式定义那些运算符。
正如相等可比较要求中指定的那样x != y等效于!(x == y)。正如小于可比较要求中指定的那样x > y等效于y < x, x >= y等效于!(x < y),和x <= y等效于!(y < x).
对operator>的要求是y < x是类型x和y的T.
对operator<=的要求是y < x是类型x和y的T.
对operator>=的要求是x < y是类型x和y的T.
对operator>, operator<=,和operator>=的要求是x和y的先决条件在operator<.
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);
}
}