是“布尔短路”吗? 由标准决定还是主要用作优化?

发布于 2024-07-18 22:01:15 字数 262 浏览 11 评论 0原文

考虑一下

Class* p = NULL;
if( p != NULL && p->Method() == OK ){

  // stuff
}

在我使用过的所有编译器上,这是相当安全的。 即,布尔表达式的第一部分将计算为false,因此不会尝试调用 Method(),因为计算第二部分是多余的。

这是因为大多数编译器都会优化第二部分的评估,还是 C/C++ 标准规定的行为?

Consider this

Class* p = NULL;
if( p != NULL && p->Method() == OK ){

  // stuff
}

On all compilers I've worked with, this is quite safe. I.e. the first part of the boolean expression will evaluate to false, and the call to Method() will thus not be attempted since evaluating the second part is redundant.

Is this because most compilers will optimize away the evaluation of the second part, or is it a dictated behavior from the C/C++ standards?

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

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

发布评论

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

评论(6

ゃ人海孤独症 2024-07-25 22:01:15

这称为布尔短路,并在许多语言中都有定义。 这里是一篇维基百科文章,描述了哪些语言具有此功能。

现在您已经知道了该功能的正确名称,还有关于它的其他 SO 文章

This is called boolean short circuiting and is defined into many languages. Here is a wikipedia article that describes which languages have this feature.

Now that you know the correct name for the feature, there are other SO articles about it as well.

再浓的妆也掩不了殇 2024-07-25 22:01:15

标准保证了表达式的捷径。

Expression short cutting is guaranteed by the standard.

埋情葬爱 2024-07-25 22:01:15

С++ 标准 1998
第5.14节

&& 运算符组从左到右。
操作数都是隐式的
转换为 bool 类型(第 4 条)。 这
如果两个操作数都为,则结果为 true
否则为真或假。 与&不同,&&
保证从左到右评估:
如果满足以下条件,则不计算第二个操作数:
第一个操作数为 false。

С++ Standard 1998
Section 5.14

The && operator groups left-to-right.
The operands are both implicitly
converted to type bool(clause 4). The
result is true if both operands are
true and false otherwise. Unlike &, &&
guarantees left-to-right evaluation:
the second operand is not evaluated if
the first operand is false.

终止放荡 2024-07-25 22:01:15

我还没有看到它被提及,所以:

C++ 保证短路,除非调用的 &&|| 运算符重载。 但不要这样做,因为这太混乱了。

I haven't seen it mentioned yet, so:

Short-circuiting is guaranteed by C++ except when the && or || operator being invoked is overloaded. But don't do that, because it's too confusing.

鲸落 2024-07-25 22:01:15

这是一个称为短路的功能。 C++ 标准保证了此行为。 我不认为这是一种优化,但它更像是一种语言功能。

This is a feature called short circuiting. This behavior is guaranteed by the C++ standard. I do not believe it is an optimization so to speak but it's much more of simply a language feature.

℡Ms空城旧梦 2024-07-25 22:01:15

它不仅仅是优化,它很有用,可以让你变得更加简洁。

正如您的示例所示,它允许您在一行中编写“安全”解除引用语句。 否则,您必须执行以下操作:

if (p != null) {
    if (p.getValue() == 3) {
        // do stuff
    }
}

看似微不足道,但尝试使用没有它的语言(例如 VB6)进行编码,您就会开始非常怀念它。

正如其他答案提到的那样,它在语言标准中,但这只是因为需要明确指定这样的东西。 它可能会编译为优化的代码,这是一个副作用; 如今,一个像样的 C 或 C++ 编译器会同等地编译一行或两行语句

It's not just optimization, it's useful to let you be more concise.

As your example shows, it lets you write a "safe" dereferencing statement in one line. Otherwise, you have to do something like:

if (p != null) {
    if (p.getValue() == 3) {
        // do stuff
    }
}

Seems trivial, but try coding in a language that doesn't have it (e.g. VB6) and you begin to sorely miss it.

It's in the language standards as other answers mention, but only because something like this needs to be clearly specified. That it can possibly compile down to optimized code is a side-effect; these days, a decent C or C++ compiler would compile the one-line or two-line statements equivilently

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