I\'m running into a strange behavior with the new spaceship operator <=>
in C++20. I\'m using Visual Studio 2019 compiler with /std:c++latest
This is by design.
[class.compare.default] (emphasis mine)
3 If the class definition does not explicitly declare an
==
operator function, but declares a defaulted three-way comparison operator function, an==
operator function is declared implicitly with the same access as the three-way comparison operator function. The implicitly-declared==
operator for a class X is an inline member and is defined as defaulted in the definition of X.
Only a defaulted <=>
allows a synthesized ==
to exist. The rationale is that classes like std::vector
cannot use a defaulted <=>
. In addition, using <=>
for ==
is not the most efficient way to compare vectors. <=>
must give the exact ordering, whereas ==
may bail early by comparing sizes first.
If a class does something special in its three-way comparison, it will likely need to do something special in its ==
. Thus, instead of generating a non-sensible default, the language leaves it up to the programmer.