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 code compiles fine, as expected:
#include <compare>
struct X
{
int Dummy = 0;
auto operator<=>(const X&) const = default; // Default implementation
};
int main()
{
X a, b;
a == b; // OK!
return 0;
}
However, if I change X to this:
struct X
{
int Dummy = 0;
auto operator<=>(const X& other) const
{
return Dummy <=> other.Dummy;
}
};
I get the following compiler error:
error C2676: binary '==': 'X' does not define this operator or a conversion to a type acceptable to the predefined operator
I tried this on clang as well, and I get similar behavior.
I would appreciate some explanation on why the default implementation generates operator==
correctly, but the custom one doesn't.
non-defaulted operator <=> doesn't generate == and !=
. I happened to encounter the motivation behind p1185r2 and was going to ask a similar question and answer it myself. – Improvised...did you mean to add bool operator==(const X&) const = default;
? – Transmigrate