Search code examples
c++language-lawyerc++20fold-expressionspaceship-operator

Can spaceship operator be used in fold expressions?


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...


Solution

  • 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.