C++ 中整数比较运算符是否短路?

发布于 2024-10-11 14:22:02 字数 80 浏览 5 评论 0原文

与标题所述一样,整数(或任何数字数据类型,如浮点数等)比较运算符(==、!=、>、>=、<、<=)在 C++ 中是否短路?

Like the title states, are the integer (or any numerical datatypes like float etc.) comparison operators (==, !=, >, >=, <, <=) short circuited in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2024-10-18 14:22:02

他们不能短路。要知道 x == yx != y 等是 true 还是 false,您需要评估x 和 y 两者。短路是指逻辑布尔运算符&&||。如果第一个参数为 false,则已知逻辑 AND 为 false;如果第一个参数为 true,则已知逻辑 OR 为 true。在这些情况下,您不需要评估第二个参数,这称为短路。

编辑:下面讨论了为什么当操作数是无符号整数且 x 为零时 x >= y 不会短路:

对于逻辑操作数来说,短路是免费的,并且与实现无关。 if(f() && g()) stmt; 的机器代码可能与此类似:

call f
test return value of f
jump to next on zero
call g
test return value of g
jump to next on zero
execute stmt
next: ...

为了防止短路,您实际上需要计算操作员的结果并在之后进行测试。这需要您一个寄存器并降低代码的效率。

对于非逻辑运算符,情况则相反。强制短路意味着:

  • 编译器无法选择使用最少数量寄存器的表达式的计算。
  • 在许多情况下,语义可能是实现定义的(甚至是未定义的),例如与最大值进行比较时。
  • 编译器需要添加额外的测试/跳转。对于 if(f() > g()) stmt; 机器代码将如下所示:

    调用 f
    mov 返回 f 的值到 R1
    测试 f 的返回值
    从零跳到下一个
    打电话给g
    将 R1 与 g 的返回值进行比较
    小于等于时跳转到下一个
    执行命令
    下一个: ...
    

    请注意,否则第一个测试和跳转是不必要的。

They can't short circuit. To know if x == y, x != y, etc are true or false 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 and x 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:

call f
test return value of f
jump to next on zero
call g
test return value of g
jump to next on zero
execute stmt
next: ...

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 can't choose an evaluation of the expression that uses a minimum number of registers.
  • The semantics may be implementation defined (or even undefined) for many cases, like when comparing with maximum value.
  • The compiler needs to add an additional test/jump. For if(f() > g()) stmt; the machine code will look like this:

    call f
    mov return value of f to R1
    test return value of f
    jump to next on zero
    call g
    compare R1 with return value of g
    jump to next on less-than equal
    execute stmt
    next: ...
    

    Note how the first test and jump are just unnecessary otherwise.

我乃一代侩神 2024-10-18 14:22:02

不可以。比较运算符需要两个操作数才能计算出正确的答案。相比之下,逻辑运算符 &&|| 在某些情况下不需要计算正确的操作数来获得正确的答案,因此会“短” -电路”。

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".

べ映画 2024-10-18 14:22:02

不,他们怎么可能。为了检查是否 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++)

若无相欠,怎会相见 2024-10-18 14:22:02

那会如何运作呢?短路意味着您可以避免仅根据评估 LHS 的结果来评估 RHS。

例如

true || false

不需要评估 RHS,因为 true ||无论x 结果是什么,x 都是true

但这不适用于您列出的任何比较。例如:

5 == x

如果不知道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.

true || false

doesn't need to evaluate the RHS because true || x is true no matter what x turns out to be.

But this won't work for any of the comparisons that you list. For example:

5 == x

How can you ever know the result of the expression without knowing x?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文