14

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • 8
    "But for any other `<=,>=,==,!=,<,>` it works." [It has been proposed](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0313r0.html) that **all** comparison operators be removed from fold expressions, as their meanings are very unintuitive. After the first comparison, you would be comparing all other values against a `bool`. – Drew Dormann Aug 06 '21 at 14:49
  • I wonder: what should be outcome for `return (a<=> ... <=>0);`? What kind of code it should generate? Can you provide also working example for `<=` which make sense? – Marek R Aug 06 '21 at 15:02
  • 6
    "But for any other `<=,>=,==,!=,<,>` it **works**." I wouldn't say it *works*, I would say it simply compiles. – Barry Aug 06 '21 at 15:22

1 Answers1

16

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982