C++ 如何处理&&?(短路评估)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5211961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does C++ handle &&? (Short-circuit evaluation)
提问by Aleksandrs Ulme
When encountering a (bool1 && bool2), does c++ ever attempts to check bool2 if bool1 was found false or does it ignore it the way PHP does?
当遇到 (bool1 && bool2) 时,如果发现 bool1 为 false,c++ 是否会尝试检查 bool2 还是像 PHP 那样忽略它?
Sorry if it is too basic of a question, but I really could not find a mentioning of that neither in Schildt nor on the Internet.
对不起,如果这是一个太基本的问题,但我真的在 Schildt 和 Internet 上都找不到提及。
回答by jason
Yes, the &&
operator in C++ uses short-circuit evaluationso that if bool1
evaluates to false
it doesn't bother evaluating bool2
.
是的,&&
C++ 中的运算符使用短路求值,因此 ifbool1
求值为false
它不会打扰求值bool2
。
"Short-circuit evaluation" is the fancy term that you want to Google and look for in indexes.
“短路评估”是你想用谷歌搜索并在索引中查找的花哨术语。
回答by Tim
C++ does use short-circuit logic, so if bool1
is false, it won't need to check bool2
.
C++ 确实使用了短路逻辑,因此如果bool1
为 false,则不需要检查bool2
.
This is useful if bool2 is actually a function that returns bool, or to use a pointer:
如果 bool2 实际上是一个返回 bool 的函数,或者使用指针,这很有用:
if ( pointer && pointer->someMethod() )
without short-circuit logic, it would crash on dereferencing a NULL pointer, but with short-circuit logic, it works fine.
如果没有短路逻辑,它会在取消引用 NULL 指针时崩溃,但使用短路逻辑,它可以正常工作。
回答by Mat
That is correct (short-cicuit behavior). But beware: short-circuiting stops if the operator invoked is not the built-in operator, but a user-defined operator&&
(same with operator||
).
这是正确的(短路行为)。但要注意:如果调用的运算符不是内置运算符,而是用户定义的operator&&
(与 相同operator||
),则短路停止。
回答by wkl
The &&
operator short circuits in C++ - if bool1
was false in your example, bool2
wouldn't be checked/executed.
&&
C++ 中的运算符短路 - 如果bool1
在您的示例中为 false,则bool2
不会被检查/执行。
回答by trev
This is called short-circuit evaluation (Wikipedia)
这称为短路评估(维基百科)
The && operator is a short circuit operator in C++ and it will not evaluate bool2 if bool1 is false.
&& 运算符是 C++ 中的短路运算符,如果 bool1 为假,它不会评估 bool2。
回答by TCSGrad
Short-circuit evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: for instance, when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.
短路求值表示某些编程语言中某些布尔运算符的语义,其中仅在第一个参数不足以确定表达式的值时才执行或评估第二个参数:例如,当 AND 的第一个参数时函数评估为假,整体值必须为假;并且当 OR 函数的第一个参数计算为真时,整体值必须为真。
In C++, both && and || operators use short-circuit evaluation.
在 C++ 中,&& 和 || 运算符使用短路评估。
回答by TheTFo
What you're referring to is short circuit evaluation. I thought that it may be compiler specific, however that article I linked to shows it as language specific, and C++ does adhere. If it is indeed compiler specific, I can't imagine a compiler that wouldn't follow it. The day to day compiler I use at the moment, VS 2008, does. Basically it will follow the operator precedence, and as soon as the condition result is guaranteed,
你指的是短路评估。我认为它可能是特定于编译器的,但是我链接到的那篇文章将其显示为特定于语言,并且 C++ 确实坚持。如果它确实是特定于编译器的,我无法想象一个不会遵循它的编译器。我目前使用的日常编译器 VS 2008 确实如此。基本上它会遵循运算符优先级,并且一旦条件结果得到保证,