Document number: P0313R0

Target audience: EWG
Ville Voutilainen

Comparison operators in fold-expressions


Comparison operators don't make much sense in fold-expressions; they expand into expressions that have surprising effects, and are thus useful for dsl-metaprogrammers only. This paper proposes removing comparison operators from the set of fold-operators.


It would be nice to be able to fix expressions like a < b < c. That would require a time machine. Not repeating the problem for fold-expressions seems doable.

Solution options

The solution options I enumerated on the reflector were as follows:

  1. it's fine, let's leave it be
  2. remove all of == != < > <= >= from the list of supported fold-operators
  3. redefine those fold-operators so that they produce a logical-and combination of individual comparisons

Based on the reflector discussion, the last option might be tricky to specify, and might have surprising semantics. Some find having different semantics for it and the bare chain of multiple comparison operators potentially confusing. The safe choice is the second one.

Rationale for removal

There's no sane way to write a fold-expression using a comparison operator correctly. The way it works is not useful for non-expert programmers and is a source of surprise (based on real field experience). It's at best only a dsl-metaprogramming tool.


In [expr.prim.fold]/1, edit as follows:

fold-operator: one of
+ - * / % & | << >>
+= -= *= /= %= = &= |= <<= >>= =
== != < > <= >= && || , .* ->*