0

I'm in the process of learning how comparisons work in C++20 and I'm struggling to understand one thing: since operator<=> has to check for equality do we really need a separate operator==? For example, the following check:

a == b

could be rewritten (by the compiler) as:

a.operator<=>(b) == 0

but instead, it will still try to call operator==. Wouldn't it make more sense to deprecate operator== and just use operator<=> for both ordering and equality? In a case a class needs some custom ordering one has to implement both operator<=> and operator== which creates an additional room for error. Is there a reason there are now two ways to check for equality?

rubix_addict
  • 1,811
  • 13
  • 27
  • 1
    `<=>` synthesizes `<` and `>` and `<=` and `>=`. `==` synthesizes `!=`. The reason being that `<=>` cannot generate an efficient `==`. – Eljay Jul 18 '21 at 15:46
  • 1
    The compiler can't know using a custom `<=>` is more or less efficient than a custom `==`. It assumes it isn't. You can still tell it to do so by defining `operator` as `= default;` explicitly. – StoryTeller - Unslander Monica Jul 18 '21 at 15:48

0 Answers0