c ++,使用多个`&& s运算表达式,并且没有优先级较低的运算符 [英] c++, evaluating expressions with multiple `&&`s and no operator of lower precedence

查看:119
本文介绍了c ++,使用多个`&& s运算表达式,并且没有优先级较低的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个表达式计算多个&&运算符,并且不计算任何优先级较低的运算符(例如||?:),则该表达式将在&&之一后立即求值为0返回0,还是会完成对其余&& s的求值?

If an expression evaluates multiple && operators, and does not evaluate any operators of lower precedence (eg. ||, ?:), will the expression evaluate to 0 as soon as one of the &&s return 0, or will it finish evaluating the remaining &&s?

例如

q=0; w=1; e=1; r=1;
if(q && w && r && e) {}

q && w等于0时,此if()等于否为假(因为其余的&&都必须等于0,而与右手运算符无关)?

Will this if() evaluate to false as soon as q && w evaluates to 0 (since the remaining && must all evaluate to 0 regardless of the right hand operators)?

推荐答案

是的,一旦遇到错误的表达式,评估就会尽早终止(短路").对此有一个例外:如果&&运算符已针对相关参数类型进行了重载.强烈建议不要这样做,而且极为罕见,但是有可能发生.

Yes, evaluation will terminate early ("short circuit") as soon as a false expression is encountered. There is one exception to this: if the && operator has been overloaded for the relevant types of arguments. This is strongly discouraged and extremely rare, but could happen.

这篇关于c ++,使用多个`&& s运算表达式,并且没有优先级较低的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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