#include <compare>
struct X {
int a;
// Work
// auto operator<=>(X const&) const = default;
};
// Doesn't work
static auto operator<=>(X const& lhs, X const& rhs) {
return lhs.a <=> rhs.a;
}
int main(){
return (X{0} <=> X{1}) == 0;
return X{0} < X{1};
return X{0} > X{1};
return X{0} >= X{1};
return X{0} <= X{1};
return X{0} == X{1};
// <source>:20:17: error: invalid operands to binary expression ('X' and 'X')
// 20 | return X{0} == X{1};
// | ~~~~ ^ ~~~~
}
#Differences between operator <=> defined in and outside a struct
23 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
It's not about inside v. Outside, but defaulted vs. Not defaulted
If <=> is not defaulted, then it does not define ==
So I still have to define == if I use a non-defaulted <=> operator?
Yes
That's sad, I thought that the point of the <=> operator was to no have to define all the < > = <= >= != and just have to define a single one
I assume it is because Issues arise on wether or not equivalent should result in true or false for ==
that is the point, yes. you can always = default the individual operators you want.
i'd guess that the reasoning is that if you define a custom <=>, there's no reasonable way to know which of the other ones are meaningful or desired.
but dunno
might also be some compatibility issues
i'm sure the paper has a long exposition on the matter 😛
Personally I feel like if <=> returns a strong ordering, then == should be defined
i remember endless debates about these sorts of things
iirc they spent literally years debating this stuff
i'm sure they'll have had reasons
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
Thank you guys :)