In C++17, are fold expressions subject to short-circuiting when used with &&
or ||
as their operator? If so, where is this specified?
Yes, fold expressions using &&
or ||
as the operator can short-circuit, subject to the usual caveat that it happens for the built-in meaning, but not for an overloaded operator function.
The meaning of a fold-expression is defined in [temp.variadic]/9:
The instantiation of a fold-expression produces:
((E_1
opE_2)
op ...)
opE_N
for a unary left fold,
E_1
op(
... op(E_N_minus_1
opE_N))
for a unary right fold,
(((E
opE_1)
opE_2)
op ...)
opE_N
for a binary left fold, and
E_1
op(
... op(E_N_minus_1
op(E_N
opE)))
for a binary right fold.In each case, op is the fold-operator,....
Since the instantiation of the fold-expression is in terms of an expression containing the operator, all the normal rules for the operator, including overload resolution, order of evaluation, and short-circuiting when a built-in operator, apply.