Consistent comparison with the operator <=>
The C++ language defines six relational operators that perform comparison: ==
, !=
, <
, <=
, >
, and >=
. Although !=
can be implemented in terms of ==
, and <=
, >=
, and >
in terms of <
, you still have to implement both ==
and !=
if you want your user-defined type to support equality comparison, and <
, <=
, >
, and >=
if you want it to support ordering.
That means 6 functions if you want objects of your type—let's call it T
—to be comparable, 12 if you want them to be comparable with another type, U
, 18 if you also want values of a U
type to be comparable with your T
type, and so on. The new C++20 standard reduces this number to either one or two, or multiple of these (depending on the comparison with other types) by introducing a new comparison operator, called the three-way comparison, which is designated with the symbol <=>
, for which reason it is popularly known as the...