[英]Are fold expressions subject to short-circuiting?
In C++17, are fold expressions subject to short-circuiting when used with &&
or ||
在C ++ 17中,当与
&&
或||
使用时,折叠表达式会受到短路影响 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. 因为操作员可以短路,但通常需要注意的是内置意义,而不是重载的操作员功能。
The meaning of a fold-expression is defined in [temp.variadic]/9: fold-expression的含义在[temp.variadic] / 9中定义:
The instantiation of a fold-expression produces:
fold-expression的实例化产生:
((E_1
opE_2)
op ...)
opE_N
for a unary left fold,((E_1
opE_2)
op ...)
opE_N
for a left left fold,
E_1
op(
... op(E_N_minus_1
opE_N))
for a unary right fold,E_1
op(
... op(E_N_minus_1
opE_N))
用于一元右折,
(((E
opE_1)
opE_2)
op ...)
opE_N
for a binary left fold, and(((E
opE_1)
opE_2)
op ...)
opE_N
表示二进制左折叠,和
E_1
op(
... op(E_N_minus_1
op(E_N
opE)))
for a binary right fold.E_1
op(
... op(E_N_minus_1
op(E_N
opE)))
用于二进制右折叠。In each case, op is the fold-operator ,....
在每种情况下, op都是折叠运算符 ,....
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. 由于fold-expression的实例化是根据包含运算符的表达式进行的,因此适用于运算符的所有常规规则,包括重载决策,评估顺序和内置运算符时的短路。
While @aschepler's answer is the specifically-correct one, I'd like to repeat a life-lesson I've shared regarding another fine technical point (tuple order of destruction) : 虽然@ aschepler的答案是特别正确的答案 ,但我想重复我关于另一个精细技术要点(破坏的元组顺序)的生活教训:
If you can formulate, for multiple alternatives, a reasonable argument for why that alternative should be the one mandated by the standard - then you should not assume any of them is mandated (even if one of them happens to be).
如果您可以为多种替代方案制定一个合理的论据,说明为什么该替代方案应该是标准规定的方案 - 那么您不应该假设它们中的任何一个是强制性的(即使其中一个恰好是)。
In the context of fold expressions and short-circuit logic - it's already difficult enough to read variadic templated codel so I'd try saving the reader of my code the head-scratching regarding whether or not fold-short-circuiting happens... 在折叠表达式和短路逻辑的上下文中 - 读取可变模板化代码已经很困难了,所以我试着保存我的代码的读者,关于是否发生折叠短路问题......
If you can't avoid writing and
and or
folds, at least be generous with commenting regarding short-circuit behavior. 如果你不能避免写作
and
/ or
折叠,至少要慷慨评论短路行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.