可以在折叠表达式中使用子表达式吗?

Can you use a subexpression within fold expressions?

下面是合法的折叠表达式吗?

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

clang but not gcc

编译

Clang 正在做正确的事情,Folding expressions Proposal 中的语法如下:

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

并包含适用于此案例的以下措辞(强调我的):

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.

(Ix < N) 确实是一个转换表达式,所以这看起来是有效的。我们可以从 5:

部分的语法中看到将我们带到那里的链条
cast-expression -> unary-expression -> postfix-expression -> 
 primary-expression -> (expression)

T.C。指出以下 gcc 错误报告 [c++1z] "binary expression in operand of fold-expression" error when folding an expression 报告了类似的问题,但仍未得到证实。

看起来这在 gcc 中对于二进制左折叠和右折叠也有问题,例如:

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

和:

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

GCC 错误。这是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) 的形式是 ( expression ),因此它是一个 主表达式 ,因此它是一个 后缀表达式,因此它是一个一元表达式,因此它是一个强制转换表达式,因此它可以用作操作数折叠表达式.