non-defaulted operator <=> doesn't generate == and != in C++20

前端 未结 3 399
野性不改
野性不改 2020-12-30 18:25

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

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 18:59

    This is by design.

    [class.compare.default] (emphasis mine)

    3 If the class definition does not explicitly declare an == operator function, but declares a defaulted three-way comparison operator function, an == operator function is declared implicitly with the same access as the three-way comparison operator function. The implicitly-declared == operator for a class X is an inline member and is defined as defaulted in the definition of X.

    Only a defaulted <=> allows a synthesized == to exist. The rationale is that classes like std::vector cannot use a defaulted <=>. In addition, using <=> for == is not the most efficient way to compare vectors. <=> must give the exact ordering, whereas == may bail early by comparing sizes first.

    If a class does something special in its three-way comparison, it will likely need to do something special in its ==. Thus, instead of generating a non-sensible default, the language leaves it up to the programmer.

提交回复
热议问题