There are some meaningful answers here on the difference, but Herb Sutter in his paper specifically says:
<=> is for type implementers: User code (including generic code) outside the implementation of an operator<=> should almost never invoke an <=> directly (as already discovered as a good practice in other languages);
So even if there was no difference, the point of the operator is different: to aid class writers to generate comparison operators.
The core difference between the subtraction operator and the "spaceship" operator (according to Sutter's proposal) is that overloading operator-
gives you a subtraction operator, whereas overloading operator<=>
:
- gives you the 6 core comparison operators (even if you declare the operator as
default
: no code to write!);
- declares whether your class is comparable, is sortable, and whether the order is total or partial (strong/weak in Sutter's proposal);
- allows for heterogeneous comparisons: you can overload it to compare your class to any other type.
Other differences are in the return value: operator<=>
would return an enum
of a class, the class specifies whether the type is sortable and whether the sort is strong or weak. The return value would convert to -1, 0 or 1 (though Sutter leaves room for the return type to also indicate distance, as strcmp
does). In any case, assuming the -1, 0, 1 return value, we'll finally get a true signum function in C++! (signum(x) == x<=>0
)
<=>
to be a non-type template parameter. This operator has consequences beyond being a replacement for one operation that "works" only on arithmetic types. – Miele(2 ** sizeof(char)) * (2 ** sizeof(char))
comparisons you need to do.) – Sigman(2 ** (sizeof(char) * CHAR_BIT))
? – Cumulous*a == *b && *a != 0
then we already know that*b != 0
, so we shouldn't include that check in the loop. – Feliks