如何在 C++20 中编写自定义飞船操作符

问题描述

我不明白为什么这段代码不能编译。根据某些网站的说法,a == b该改写为 a.operator<=>(b) == 0,但是 clang 和 gcc 都无法编译。

#include <utility>
#include <stdio.h>

struct A {
public:
    int x_ = 0;

public:
    std::strong_ordering operator<=>(const A& o) const 
    {
        return int(x_ == o.x_ ? 0 : x_ < o.x_ ? -1 : 1) <=> 0;
    }
};

int main()
{
  A a{1};
  A b{2};

  if(a == b) printf("a==b\n");
}

但是,如果您使用认值,则一切正常!

...
   std::strong_ordering operator<=>(const A& o) const = default;
...

所以我的问题是,您如何手动编写 default<=> 实现?

解决方法

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

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

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