How to use three-way comparison (spaceship op) to implement operator== between different types?
The premise of the question is wrong. You don't use the three-way comparison operator (<=>
) to implement ==
: you use ==
to implement ==
:
bool operator==(type_a a, type_b b) {
return a.member == b.member;
}
The source of confusion is that there is one exception to this rule: if a type declares a defaulted <=>
then it also declares a defaulted ==
:
struct type_c {
int member;
auto operator<=>(type_c const&) const = default;
};
That declaration is equivalent to having written:
struct type_c {
int member;
bool operator==(type_c const&) const = default;
auto operator<=>(type_c const&) const = default;
};
But it's not the <=>
that gives you ==
: it's still the ==
, and only ==
, that gives you ==
.