Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use a subexpression within fold expressions?

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

like image 546
Ryan Burn Avatar asked Nov 17 '15 02:11

Ryan Burn


2 Answers

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) );
like image 147
Shafik Yaghmour Avatar answered Oct 14 '22 16:10

Shafik Yaghmour


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.

like image 33
T.C. Avatar answered Oct 14 '22 16:10

T.C.