3 方向の演算子オーバーライドを持つクラスの等価演算でコンパイラ エラーが発生する理由を知っている人はいますか? 私はVS2019を使用しています。
class Rectangle
{
public:
constexpr Rectangle(const int width, const int height) :
width{ width }, height{ height } { }
auto operator<=>(const Rectangle& rhs) const {
return width * height <=> rhs.width * rhs.height;
}
int width;
int height;
};
void Test() {
Rectangle r1(5, 10);
Rectangle r2(10, 5);
auto ret1 = r1 < r2;
auto ret2 = r1 <= r2;
auto ret3 = r1 == r2; // error on this line but previous two are good
}
上記の行でエラーが発生します。
編集: C++20 のデフォルトでない演算子 <=> は == と != を生成しないの説明では、不等号演算子が機能するのに等号演算子が機能しない理由に対処していないことに注意してください。
編集: 上記のリンクは、1 か月後に再読した後、今では理にかなっています。