I don't understand why this code doesn't compile. According to some websites, a == b
should be rewritten as a.operator<=>(b) == 0
, however both clang and gcc fail to compile.
#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");
}
However, if you use the default, everything works!
...
std::strong_ordering operator<=>(const A& o) const = default;
...
So my question is, how do you write the default
implementation of <=>
by hand?