In the original design of the spaceship operator, ==
is allowed to call <=>
, but this is later disallowed due to efficiency concerns (<=>
is generally an inefficient way to implement ==
). operator<=>() = default
is still defined to implicitly define operator==
, which correctly calls ==
instead of <=>
on members, for convenience. So what you want is this:
struct A {
int n;
auto operator<=>(const A& rhs) const noexcept = default;
};
// ^^^ basically expands to vvv
struct B {
int n;
bool operator==(const B& rhs) const noexcept
{
return n == rhs.n;
}
auto operator<=>(const B& rhs) const noexcept
{
return n <=> rhs.n;
}
};
Note that you can independently default operator==
while still providing a user-defined operator<=>
:
struct B {
int n;
// note: return type for defaulted equality comparison operator
// must be 'bool', not 'auto'
bool operator==(const B& rhs) const noexcept = default;
auto operator<=>(const B& rhs) const noexcept
{
return n <=> rhs.n;
}
};
==
doesn't include<=>
) – Squamation