None of the compilers I tried accept such code:
template <int ...a> bool foo() { return (a<=> ... <=>0); }
But for any other <=,>=,==,!=,<,>
it compiles.
cppreference is clear here - there is no <=>
on the list of binary operators we can use for fold expression.
Is this an intentional omission in the C++ standard, or are compilers not ready with this?
The question is just pure curiosity; I just wanted to know what the C++ direction is in this area. I can imagine all other compare operators will be removed from the fold-expression list of allowed operators, as they have as much sense as <=>
in a fold expression...
In PHP 7, a new feature, spaceship operator has been introduced. It is used to compare two expressions. It returns -1, 0 or 1 when first expression is respectively less than, equal to, or greater than second expression.
In C++, the C++20 revision adds the "spaceship operator" <=> , which similarly returns the sign of the difference and can also return different types (convertible to signed integers) depending on the strictness of the comparison.
A fold expression is an instruction for the compiler to repeat the application of an operator over a variadic template pack. Let's take an example. A very basic one and with a questionable usefulness, but one that illustrates how fold expressions work.
The C++20 three-way comparison operator <=> (commonly nicknamed the spaceship operator due to its appearance) compares two items and describes the result. It's called the three-way comparison because there are five possible results: less, equal, equivalent, greater, and unordered.
This is intentional.
The problem with fold-expanding comparison operators is that it works by doing this: A < B < C < D
. This is only meaningfully useful in circumstances where operator<
has been overloaded to mean something other than comparison. This is why an attempt was made to stop C++17 from allowing you to fold over them in the first place.
operator<=>
is never supposed to be used for something other than comparison. So it is forbidden.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With