C++ 中整数比较运算符是否短路?
与标题所述一样,整数(或任何数字数据类型,如浮点数等)比较运算符(==、!=、>、>=、<、<=)在 C++ 中是否短路?
Like the title states, are the integer (or any numerical datatypes like float etc.) comparison operators (==, !=, >, >=, <, <=) short circuited in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他们不能短路。要知道
x == y
、x != y
等是true
还是false
,您需要评估x 和 y 两者。短路是指逻辑布尔运算符&&
和||
。如果第一个参数为 false,则已知逻辑 AND 为 false;如果第一个参数为 true,则已知逻辑 OR 为 true。在这些情况下,您不需要评估第二个参数,这称为短路。编辑:下面讨论了为什么当操作数是无符号整数且
x
为零时x >= y
不会短路:对于逻辑操作数来说,短路是免费的,并且与实现无关。
if(f() && g()) stmt;
的机器代码可能与此类似:为了防止短路,您实际上需要计算操作员的结果并在之后进行测试。这需要您一个寄存器并降低代码的效率。
对于非逻辑运算符,情况则相反。强制短路意味着:
编译器需要添加额外的测试/跳转。对于
if(f() > g()) stmt;
机器代码将如下所示:请注意,否则第一个测试和跳转是不必要的。
They can't short circuit. To know if
x == y
,x != y
, etc aretrue
orfalse
you need to evaluate both, x and y. Short circuiting refers to logical boolean operators&&
and||
. Logical AND is known to be false if the first argument is false and Logical OR is known to be true if the first argument is true. In these cases you don't need to evaluate the second argument, this is called short circuiting.Edit: this follows the discussions for why
x >= y
don't short circuit when the operands are unsigned ints andx
is zero:For logical operands short circuiting comes for free and is implementation neutral. The machine code for
if(f() && g()) stmt;
is likely to look similar to this:To prevent short circuiting you actually need to do the computation of the result of the operator and test it after that. This takes you a register and makes the code less efficient.
For non-logical operators the situation is the opposite. Mandating short circuiting implies:
The compiler needs to add an additional test/jump. For
if(f() > g()) stmt;
the machine code will look like this:Note how the first test and jump are just unnecessary otherwise.
不可以。比较运算符需要两个操作数才能计算出正确的答案。相比之下,逻辑运算符
&&
和||
在某些情况下不需要计算正确的操作数来获得正确的答案,因此会“短” -电路”。No. The comparison operators require both operands to evaluate the correct answer. By contrast, the logical operators
&&
and||
in some cases don't need to evaluate the right operand to get the right answer, and therefore do "short-circuit".不,他们怎么可能。为了检查是否
1 == 2
你必须检查 1 和 2。(Ofcoruse,编译器可以做很多重新排序、静态检查、优化等,但这不是继承到 c++)No, how could they be. In order to check whether
1 == 2
you have to inspect both the 1 and the 2. (Ofcoruse, a compiler can do a lot of reordering, static checking, optimizations, etc. but that's not inherit to c++)那会如何运作呢?短路意味着您可以避免仅根据评估 LHS 的结果来评估 RHS。
例如
不需要评估 RHS,因为
true ||无论
都是x
结果是什么,xtrue
。但这不适用于您列出的任何比较。例如:
如果不知道
x
,你怎么能知道表达式的结果呢?How would that work? Short-circuiting means you can avoid evaluating the RHS based solely on the result of evaluating the LHS.
e.g.
doesn't need to evaluate the RHS because
true || x
istrue
no matter whatx
turns out to be.But this won't work for any of the comparisons that you list. For example:
How can you ever know the result of the expression without knowing
x
?