问题描述
在C ++ 17中,折叠表达式在使用时会受到的约束使用& 或 || 作为其运算符?如果是,则在哪里指定?
In C++17, are fold expressions subject to short-circuiting when used with && or || as their operator? If so, where is this specified?
推荐答案
是的,使用&& 或 || ,因为运算符可以短路,但要遵守通常的警告,即内在含义,但不适用于重载运算符
Yes, fold expressions using && or || as the operator can short-circuit, subject to the usual caveat that it happens for the built-in meaning, but not for an overloaded operator function.
折叠表达式的含义在[temp.variadic] / 9中定义:
The meaning of a fold-expression is defined in [temp.variadic]/9:
-
(((E_1 op E_2) op ... ) op E_N 一元左折,
((E_1 op E_2) op ...) op E_N for a unary left fold,
E_1 op ( ... op (E_N_minus_1 op E_N))一元右折,
E_1 op (... op (E_N_minus_1 op E_N)) for a unary right fold,
((((E op E_1) op E_2) op ... ) op E_N 进行二进制左折,并且
(((E op E_1) op E_2) op ...) op E_N for a binary left fold, and
E_1 op ( ... op (E_N_minus_1 op (E_N op E)))进行二进制右折。
E_1 op (... op (E_N_minus_1 op (E_N op E))) for a binary right fold.
每个情况下, op 是 fold-operator ,...。
In each case, op is the fold-operator,....
fold-expression用包含运算符的表达式表示,该运算符的所有常规规则都适用,包括重载解析,求值顺序和适用于内置运算符时的短路。
Since the instantiation of the fold-expression is in terms of an expression containing the operator, all the normal rules for the operator, including overload resolution, order of evaluation, and short-circuiting when a built-in operator, apply.
这篇关于折叠表达式会发生短路吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!