为什么飞船操作员不允许我执行相等运算但允许不等运算?

问题描述

有谁知道为什么我在一个具有 3 向运算符覆盖的类上执行相等运算时出现编译器错误?我使用的是 VS 2019。

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
}

我收到上述行的错误: NativeConsoleApp.cpp(68,20): error C2676: binary '==': 'Rectangle' 未定义此运算符或转换为预定义运算符可接受的类型

编辑:请注意 non-defaulted operator <=> doesn't generate == and != in C++20 处的解释没有说明为什么不等式运算符起作用,而等式运算符不起作用。

编辑:上面的链接一个月后重读后才有意义。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)