布尔的C ++短路 [英] C++ short-circuiting of booleans

查看:102
本文介绍了布尔的C ++短路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的新手,我很好奇编译器如何处理boolean的延迟评估。例如,

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

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

如果A等于1,是B == 2部分曾评估过吗?

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

推荐答案

不, B == 2 零件不评估。这称为短期评估

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

修改:作为 Robert C. Cartaino正确地指出,如果逻辑运算符过载,则不会发生短路评估(已经说过,为什么有人会超载逻辑运算符超过我)。

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

这篇关于布尔的C ++短路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆