C++布尔值短路

发布于 2024-08-12 06:34:15 字数 125 浏览 5 评论 0原文

我是 C++ 新手,很好奇编译器如何处理布尔值的惰性求值。例如,

if(A == 1 || B == 2){...}

如果 A 等于 1,则 B==2 部分是否已计算?

I'm new to c++ and am curious how the compiler handles lazy evaluation of booleans. For example,

if(A == 1 || B == 2){...}

If A does equal 1, is the B==2 part ever evaluated?

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

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

发布评论

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

评论(8

暮色兮凉城 2024-08-19 06:34:15

No, the B==2 part is not evaluated. This is called short-circuit evaluation.

Edit: As Robert C. Cartaino rightly points out, if the logical operator is overloaded, short-circuit evaluation does not take place (that having been said, why someone would overload a logical operator is beyond me).

無處可尋 2024-08-19 06:34:15

除非||运算符重载,否则不会计算第二个表达式。这称为“短路评估”。

在逻辑 AND (&&) 和逻辑 OR (||) 的情况下,如果第一个表达式足以确定整个表达式的值,则不会计算第二个表达式。

在上面描述的情况下:

if(A == 1 || B == 2) {...}

...第二个表达式将不会被计算,因为

TRUE || ANYTHING,始终评估为TRUE

同样,

FALSE && ANYTHING,始终评估为 FALSE,因此该情况​​也会导致

一些简短说明

  • 短路评估不适用于重载的&&|| 运算符。
  • 在 C++ 中,您保证将首先计算第一个表达式。有些语言不保证求值的顺序,VB 根本不做短路求值。了解您是否正在移植代码非常重要。

Unless the || operator is overloaded, the second expression will not be evaluated. This is called "short-circuit evaluation."

In the case of logical AND (&&) and logical OR (||), the second expression will not be evaluated if the first expression is sufficient to determine the value of the entire expression.

In the case you described above:

if(A == 1 || B == 2) {...}

...the second expression will not be evaluated because

TRUE || ANYTHING, always evaluates to TRUE.

Likewise,

FALSE && ANYTHING, always evaluates to FALSE, so that condition will also cause a short-circuit evaluation.

A couple of quick notes

  • Short circuit evaluation will not apply to overloaded && and || operators.
  • In C++, you are guaranteed that the first expression will be evaluated first. Some languages do not guarantee the order of evaluation and VB doesn't do short-circuit evaluation at all. That's important to know if you are porting code.
诗化ㄋ丶相逢 2024-08-19 06:34:15

B==2 部分不被评估。

小心!不要把 ++B==2 之类的东西放在那里!

The B==2 part is not evaluated.

Be careful! Don't put something like ++B==2 over there!

夜清冷一曲。 2024-08-19 06:34:15

C++ 将短路应用于布尔表达式求值,因此,B == 2 永远不会被评估,编译器甚至可能完全忽略它。

C++ applies short circuiting to Boolean expression evaluation so, the B == 2 is never evaluated and the compiler may even omit it entirely.

删除→记忆 2024-08-19 06:34:15

编译器通过生成中间跳转来处理这个问题。对于以下代码:

if(A == 1 || B == 2){...}

编译为伪汇编程序,可能是:

    load variable A
    compare to constant 1
    if equal, jump to L1
    load variable B
    compare to constant 2
    if not equal, jump to L2
L1:
    ... (complete body of if statement)
L2:
    (code after if block goes here)

The compiler handles this by generating intermediate jumps. For the following code:

if(A == 1 || B == 2){...}

compiled to pseudo-assembler, might be:

    load variable A
    compare to constant 1
    if equal, jump to L1
    load variable B
    compare to constant 2
    if not equal, jump to L2
L1:
    ... (complete body of if statement)
L2:
    (code after if block goes here)
素手挽清风 2024-08-19 06:34:15

正如 James 所说,这是短路评估惰性评估是完全不同的东西。

This is short-circuit evaluation, as James says. Lazy evaluation is something entirely different.

后来的我们 2024-08-19 06:34:15

不,不是。

&& 相同,如果其中一个错误,则不会费心评估另一个。

No it's not.

Same with &&, if one is wrong, it doesn't bother evaluating the other one.

戏剧牡丹亭 2024-08-19 06:34:15

B == 2 永远不会被评估。

有关详细信息,请参阅短路评估

B == 2 is never evaluated.

See Short-Circuit Evaluation for more information.

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