Ohh, the Spaceship operator <=> is coming to C++20.
This allows you to define a single three-way comparison operator <=>, and the compiler can auto generate all other comparison operators from it: ==, !=, <, >, <=, >=
Example (from cppreference.com):
class Point {
int x;
int y;
public:
auto operator<=>(const Point&) const = default;
/* ... non-comparison functions ... */
};
/* compiler generates all six relational operators */
Point pt1, pt2;
if (pt1 == pt2) { /*...*/ } /* ok */
std::set<Point> s; /* ok */
s.insert(pt1); /* ok */
if (pt1 <= pt2) { /*...*/ } /* ok, makes only a single call to <=> */
References:
cppreference.com - Default Comparisons (https://en.cppreference.com/w/cpp/language/default_comparisons)
cppreference.com - Comparison Operators - Three Way Comparison (https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison)
Library Support for the Spaceship (Comparison) Operator (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0768r1.pdf)
EDIT (leeor_net): Fix code syntax highlighting.
Probably by doing one operator in terms of the other.
E.g., in many implementations of operator> and operator<, the code looks something like this:
bool Class::operator>(c)
{
/* some comparison of members here */
return true;
}
Class::operator<(c)
{
return this > c;
}