为什么" ++点¯x|| ++ Y'放大器;&安培; ++ž"计算" ++ X'QUOT;第一,即使操作员QUOT;&放大器;&放大器;"具有较高的precedence比" ||" [英] Why does "++x || ++y && ++z" calculate "++x" first, even though operator "&&" has higher precedence than "||"

查看:102
本文介绍了为什么" ++点¯x|| ++ Y'放大器;&安培; ++ž"计算" ++ X'QUOT;第一,即使操作员QUOT;&放大器;&放大器;"具有较高的precedence比" ||"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 ++点¯x|| ++ Y'放大器;&安培; ++ž计算 ++ X 第一,尽管运营商的precedence &放大器;&安培; ||

解决方案

放松身心,的R 和其他人解释到底发生了什么。因此,让我补充:

你的问题的premise故障。 &安培; 已高出precedence并不意味着,围绕着它的操作数必须在EX pression任何操作数与之前评估的&安培的事实低precedence。即使在的特殊情况短路|| &放大器;&安培; 也未必如此。

例如,考虑 A = B + C + D * E ; * 具有较高的precedence比 + ,但这并不意味着 D * E 前必须评估b + C 。它只是意味着我们的产品添加到前pression作为一个整体之前,必须进行评估。编译器可以评估这个前pression为 temp1目录= D * E TEMP2 = A + B A = temp1目录+ TEMP2 或者它可以评估 temp1中= b + C TEMP2 = D * Ë A = temp1中+ TEMP2 。两者都是同样有效。

随着短路行为 || &放大器;&安培; ,还有摆放着一些额外的限制在评估顺序。


作为一个方面说明:一般情况下我会避免写code这样。我可以很容易地看到其他程序员尝试读取这个code弄不清只是当增量会发生时,他们不会。好吧,也许如果你使用的真实变量名就不会看起来那么离奇。

我偶尔也会依靠短路preventing副作用。像

 如果(!EOF()及和放大器; readNextInt())大于0

我靠短路到prevent阅读,如果我们在文件末尾是已经或

 如果(confirmDelete == YES&放大器;&安培;!deleteEntry()=  -  1)`

我依靠第一个测试假短路,所以我不做删除时,我不应该。但这些例子似乎pretty直截了当给我,我倒是希望任何有能力的程序员会看到我在做什么。但是,当例子拿到神秘,我认为它需要被打破了。考虑

 如果(customerType == ||零售lastDepositAmount()> 100.00)

如果 lastDepositAmount()有副作用,那么如果 customerType 是零售这种副作用将不会发生。我不认为这必然是显而易见的读者。 (部分原因是因为该功能顾名思义,它是检索数据并没有进行任何更新,部分是因为有客户类型和存款金额之间没有明显的关系 - 这听起来像两个独立的东西)不可否认,这是主观。但是,当有疑问,选择简单和清晰了微不足道的性能提升。总是选择简单和清晰过的嘿,这是一个非常酷使用一个不起眼的功能,任何人读这将是pssed IM $ P $以我一定要多么聪明理解的语言不够好,要做到这一点

Why does ++x || ++y && ++z calculate ++x first, even though the precedence of operator && is higher than ||?

解决方案

Unwind, R and others have explained what really happens. So let me just add:

The premise of your question is faulty. The fact that the && has higher precedence doesn't mean that operands that surround it must be evaluated before any operands in the expression with lower precedence. Even where the special case short-circuiting of || and && this wouldn't necessarily be so.

For example, consider a=b+c+d*e; * has higher precedence than +, but that doesn't mean that d*e must be evaluated before b+c. It just means that it must be evaluated before we add the product to the expression as a whole. A compiler could evaluate this expression as temp1=d*e, temp2=a+b, a=temp1+temp2 or it could evaluate temp1=b+c, temp2=d*e, a=temp1+temp2. Both would be equally valid.

With the short-circuiting behavior of || and &&, there are some additional restrictions placed on order of evaluation.


As a side note: In general I would avoid writing code like this. I can easily see another programmer trying to read this code getting confused about just when the increments will happen and when they won't. Well, maybe if you used real variable names it would not look so bizarre.

I do occasionally rely on short-circuiting preventing side effects. Like

if (!eof() && readNextInt())>0 

I'm relying on the short-circuit to prevent reading if we're already at end of file, Or

if (confirmDelete==YES && deleteEntry()!=-1)` 

I'm relying on the first test to short-circuit on false so I don't do the delete when I shouldn't. But these examples seem pretty straightforward to me, I'd hope any competent programmer would see what I'm doing. But when the examples get cryptic, I think it needs to be broken out. Consider

if (customerType==RETAIL || lastDepositAmount()>100.00)

If lastDepositAmount() had a side effect, then if customerType is retail this side effect will never happen. I don't think that would necessarily be obvious to a reader. (Partly because the function name implies that it is retrieving data and not performing any update, and partly because there is no obvious relationship between the customer type and the amount of a deposit -- these sound like two independent things.) Admittedly, this is subjective. But when in doubt, choose simplicity and clarity over a trivial performance improvement. Always choose simplicity and clarity over "hey this is a way cool use of an obscure feature, anyone reading this will be impressed at how smart I must be to understand the language well enough to do this".

这篇关于为什么" ++点¯x|| ++ Y'放大器;&安培; ++ž"计算" ++ X'QUOT;第一,即使操作员QUOT;&放大器;&放大器;"具有较高的precedence比" ||"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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