Can you use a subexpression within fold expressions?

可紊 提交于 2020-01-14 07:26:08

问题


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


回答1:


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) );



回答2:


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.



来源:https://stackoverflow.com/questions/33748391/can-you-use-a-subexpression-within-fold-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!