您可以在折叠表达式中使用子表达式吗? [英] 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) && ...);
}
推荐答案
Clang在做正确的事情,即折叠表达式提案如下:
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):
以下形式的表达式(... op e),其中op是一个折叠运算符,称为一元左折。形式为(e op ...)的表达式(其中op是折叠运算符)被称为一元右折叠。一元左折叠和一元右折叠统称为一元折叠。 在一元折叠中,强制转换表达式应包含未扩展的参数包。
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
中的语法所示:
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。指出了以下gcc错误报告 [c ++ 1z]折叠表达式时出现折叠表达式错误,报告了类似的问题,但仍未得到确认。
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.
在gcc中,对于二进制文件,它似乎也被破坏了左折和右折,例如:
It looks like this is also broken in gcc for binary left and right folds, for example:
return ( (Ix < N) && ... && (N < 10) );
和:
return ( (N < 10) && ... && (Ix < N) );
这篇关于您可以在折叠表达式中使用子表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!