for example, there is a structure with two fields:
struct point {
float x, y;
bool operator ==(point p) { return x == p.x && y == p.y; }
bool operator <(point p) { return x < p.x && y < p.y; }
bool operator <=(point p) { return x <= p.x && y <= p.y; }
bool operator >(point p) { return x > p.x && y > p.y; }
bool operator >=(point p) { return x >= p.x && y >= p.y; }
};
Comparison operators check the condition through logical AND for each variable, how should the <=> operator look like so that its result does not differ from these operators?
I already tried to move all operators to the new <=> back when compilers only began to support it, but neither then nor now have I found a way to implement what I wanted.
The only more or less working version of the code that I found:
std::partial_ordering operator<=> (const point& p) const {
std::partial_ordering c = x <=> point.x;
if(y <=> point.y != c) return std::partial_ordering::unordered;
return c;
}
It produces correct results for < and >, but incorrect results for <= and >=. For example, point(0, 0) <= point(0, 1) is true but here <=> returns unordered.
One of the issues with your relational comparisons is that they're not actually consistent. Namely:
The definition of
p <= qis(p < q) or (p == q). But that's not what's actually going on here.Considering your example where
pis(0, 0)andqis(0, 1):p == qisfalse(they're not the same)p < qisfalse(because whilep.y < q.y, it is not the case thatp.x < q.x)Yet, nevertheless:
p <= qistrue(becausep.x <= q.xand alsop.y <= q.y)That's inconsistent.
Your
operator<=>is actually correct, it just helped expose the fact that youroperator<=andoperator>=are incorrect.