Changes Between C++17 and C++20
open-std.org
open-std.org
Assorted snippets demonstrating C++20
int main() {}>>>The value of the expression a <=> b describes which one of a < b, a == b, and a > b is true.
A key part of this feature is that classes that provide this operator can have all of the six traditional, binary relational operators defined implicitly.
With operator<=> one can implement a single function and the compiler becomes the smart one. When calling o1 < o2 or calling o1 >= o2 the compiler will do a call to the single operator<=> and interpret the return value accordingly.
Notably less repitive typing for the type provider.
I found a blog post which raised similar concerns [0], and P1185 [1] proposes some solutions. I don't know whether they made it into the standard, though.
[0]: https://foonathan.net/2018/10/spaceship-proposals/
[1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p118...
bool operator<(const Foo& foo) const { if (a < foo.a) return true; if (a > foo.a) return false; if (b < foo.b) return true; if (b > foo.b) return false; return false; }
.. and all the other comparison operators that you needed.
But now, you can just say:
auto operator<=>(const Foo&) const = default;
and the compiler will automatically generate all the operators for you.
You can think of two types of comparison:
* Those that return a boolean. eg. operator < returns bool, true if less than the operand, false if greater or equal.
* Those that return a 3-way value. An example of this would be strcmp(), which might return -1 if less, 0 if equal, and 1 if greater.
The insight is that if the compiler knows the latter exists, it can fill in the former category, and the programmer doesn't have to write a bunch of extra overloads.
There are some correctness and efficiency concerns operator<=> addresses, too, but I'm not confident I can explain those well.
You can read more in P0515R0 [0], which sort of "rolls up" previous papers discussing comparisons.
[0]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p051...