10

Is the following a legal fold expression?

template <std::size_t N, std::size_t... Ix>
bool in_range(std::index_sequence<Ix...>) {
  return ((Ix < N) && ...);
}

It compiles with clang but not gcc

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Ryan Burn
  • 2,126
  • 1
  • 14
  • 35
  • 1
    I would think (without consulting official rules) `(A op ...)` is valid in that way whenever `A...` is valid. For example, `(Ix < N)...` being valid in some context would imply `((Ix < N) op ...)` being valid in some other context. – chris Nov 17 '15 at 03:06
  • Incidentally, only right folds are affected by this bug. `(... && (Ix < N))` works on GCC. – T.C. Nov 17 '15 at 04:05

2 Answers2

9

Clang is doing the correct thing, the grammar from the Folding expressions Proposal is as follows:

fold-expression:
      ( cast-expression fold-operator ... )
      ( ... fold-operator cast-expression )
      ( cast-expression fold-operator ... fold-operator cast-expression )

and contains the following wording that applies to this case (emphasis mine):

An expression of the form (... op e) where op is a fold-operator is called a unary left fold. An expression of the form (e op ...) where op is a fold-operator is called a unary right fold. Unary left folds and unary right folds are collectively called unary folds. In a unary fold, the cast-expression shall contain an unexpanded parameter pack.

and (Ix < N) is indeed a cast-expression, so this looks valid. We can see the chain that gets us there, as follows from the grammar in section 5:

cast-expression -> unary-expression -> postfix-expression -> 
 primary-expression -> (expression)

T.C. pointed out the following gcc bug report [c++1z] "binary expression in operand of fold-expression" error when folding an expression which reports a similar issue but it is still unconfirmed.

It looks like this is also broken in gcc for binary left and right folds, for example:

return ( (Ix < N) && ... &&  (N < 10) );

and:

return ( (N < 10) && ... &&  (Ix < N) );
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
4

GCC is wrong. This is bug 68377, presumably introduced by the fix for bug 67810.

fold-expression:    
    ( cast-expression fold-operator ... )
    [...]

primary-expression:    
    [...]
    ( expression )
    [...]

postfix-expression:    
    primary-expression
    [...]

unary-expression:    
    postfix-expression
    [...]

cast-expression:
    unary-expression
    [...]

(Ix < N) is of the form ( expression ), therefore it is a primary-expression, therefore it is a postfix-expression, therefore it is a unary-expression, therefore it is a cast-expression, therefore it can be used as an operand of a fold-expression.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • Thanks. I actually put in [bug 68377](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68377), but was a little less sure after investigating more. But this looks like it answers it. – Ryan Burn Nov 17 '15 at 03:19