Consider the following two overload operator<=>
for S
:
#include <compare>
struct S {};
int operator<=>(S, int) { return 0; } #1
S operator<=>(S, S) { return {}; } #2
If I compare an object S
with an int
, the #1
will generate the right operators for me, so expression like S{} <= 0
, 0 < S{}
or 0 <=> S{}
would be just fine.
But if I compare an object S
with other object S
:
S{} < S{};
Then this will be rewritten as (S{} <=> S{}) < 0
. Since (S{} <=> S{})
will return an other S
, we back to the origin problem: S
compare with a int
. At this time, we don't have operator<(S, int)
, so #1
would generate the right operator for me.
But surprisingly, none of the three compilers do this to me. GCC, Clang, and MSVC all reject S{} < S{}
with the same error message:
no match for 'operator<' (operand types are 'S' and 'int')
This makes me frustrated. Since the #1
actually exists. Why the nested generation of the operator are not occurring here? What does the standard say? Is there a static constraint violation?
<=>
is required to returnauto
,std::strong_ordering
,std::weak_ordering
,std::partial_ordering
, orbool
. en.cppreference.com/w/cpp/language/default_comparisons – NathanOliver<=>(S,S)
toauto
, which conforms to the standard (as far as I can tell), and you still get the same behavior. – cigienbool
there doesn't apply to<=>
). – Barry