stdrel_opsoperator(3)
std::rel_ops::operator!=,>,<=,>= - std::rel_ops::operator!=,>,<=,>=
Synopsis
Defined in header<utility>
template <class T>
(1)(deprecated in C++ 20)
bool
operator!=(const T &lhs, const T &rhs);
template <class T>
(2)(deprecated in C++ 20)
bool
operator>(const T &lhs, const T &rhs);
template <class T>
(3)(deprecated in C++ 20)
bool
operator<=(const T &lhs, const T &rhs);
template <class T>
(4)(deprecated in C++ 20)
bool
operator>=(const T &lhs, const T &rhs);Given a user-defined operator== and operator< for objects of type T, implements the usual semantics of other comparison operators.
- Implements operator!= in terms of operator==.
- Implements operator> in terms of operator<.
- Implements operator<= in terms of operator<.
- Implements operator>= in terms of operator<.
Parameters
lhs - left-hand argument rhs - right-hand argument
Return value
- Returns true if lhs is not equal to rhs.
- Returns true if lhs is greater than rhs.
- Returns true if lhs is less or equal to rhs.
- Returns true if lhs is greater or equal to rhs.
Possible implementation
First version
Second version
Third version
Fourth version
Notes
Boost.operators provides a more versatile alternative to std::rel_ops As of C++20, std::rel_ops are deprecated in favor of operator<=>.
Example
// Run this code
#include <iostream>
#include <utility>
struct Foo {
int n;
};
bool operator==(const Foo &lhs, const Foo &rhs)
{
return lhs.n == rhs.n;
}
bool operator<(const Foo &lhs, const Foo &rhs)
{
return lhs.n < rhs.n;
}
int main()
{
Foo f1 = {1};
Foo f2 = {2};
using namespace std::rel_ops;
std::cout << std::boolalpha;
std::cout << "not equal? : " << (f1 != f2) << ‘\n’;
std::cout << "greater? : " << (f1 > f2) << ‘\n’;
std::cout << "less equal? : " << (f1 <= f2) << ‘\n’;
std::cout << "greater equal? : " << (f1 >= f2) << ‘\n’;
}
Output: