There are some other possible syntaxes that don't necessarily involve a template argument like that. For example,
class A {};
bool operator<=(A,A) { return true; }
class B {};
bool operator>(bool(*)(A,A), B) { return false; }
int main()
{
B b;
return operator <=> b;
}
But it is true all such examples do have the keyword operator
immediately before the appearance of <=>
.
The only way to prove a claim like that is to do an exhaustive search through the entire C++ grammar, conveniently shown in one place in Appendix A of the C++17 Standard and some other Standard versions.
First, note that because of the Maximal Munch rule, if the next source characters after previous preprocessor tokens have been parsed are <=>
, then C++17 and earlier will always treat the first token as <=
. The next token could actually be >
or >>
or >=
or >>=
.
The only grammar rules involving the token <=
are:
fold-operator:
<=
relational-expression:
relational-expression <=
shift-expression
operator:
<=
The grammar symbol fold-operator is only used in:
fold-expression:
(
cast-expression fold-operator ... )
( ...
fold-operator cast-expression )
(
cast-expression fold-operator ...
fold-operator cast-expression )
So as a fold-operator, <=
must be followed by either the ...
token (which is certainly not >
or >>
or >=
or >>=
), or a cast-expression. Either as a fold-operator or in a relational-expression, the <=
token may be followed by a cast-expression or shift-expression, which are both restricted sorts of expression. But there is no grammar rule allowing any expression to begin with >
or >>
or >=
or >>=
.
That leaves just the grammar symbol operator, found only in:
operator-function-id:
operator
operator
which shows that the keyword operator
must be immediately before the <=
which might instead end up as part of a <=>
in C++20.
<=>
for this operator rather than something else, since the change won't break much code, and 2) Clang is giving a warning, since they know it won't be triggered very often. – Wisteria-std=c++20
will emit a compile-time error. – Italy<=>
in C++17, which is also syntactically valid code in C++20 but with different semantics. – Italyoperator<=>
in that example – Kogeroperator<=>
for a program like this to be valid in both C++17 and C++20. Of course, you can't declare one yourself and have it be valid C++17. And it looks like none of the standard headers declare anyoperator<=>
... yet. But probably there should be some. – Mirza