如何在语句中评估涉及postfix和prefix运算符的表达式? [英] How to evaluate expressions involving postfix and prefix operators in a statement?

查看:54
本文介绍了如何在语句中评估涉及postfix和prefix运算符的表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑i = 4

我想打印结果:++ i + ++ i + i ++

结果是什么以及如何?

解决方案

嗯,伙计,这是你的功课。答案是17,如果你有一个编译器,你可以简单地计算它,只需复制粘贴。但我不会告诉你为什么。我建议你查阅优先表 [ ^ ],并想象一下从左到右表达式树将如何看起来......你会得到它。


平均答案是:取决于

问题在于评估函数参数是未定义的。即一个编译器可能这样做,另一个是另一种方式。它甚至可能随着参数类型或参数的数量而改变。



为了说明这一点:在C ++中,运算符可以写成函数调用,例如:

  int  result = operator +(operator +(operator ++(i),operator ++(i)),operator ++(i, 0 ));  operator ++(i)是预增量运算符,而运算符++(i,0)是后增量运算符。



尝试这两种情况:

A)编译器从左到右评估参数

B)你的编译器从右到左评估参数



A)从左到右看到序列(自上而下)

 result = operator +  //   1. call  
(operator + // 2.评估第一个表达式...
(operator ++(i) // 3.评估第一个表达式:res = 5,i = 5
,operator ++(i) // 4.评估第二个表达式:res = 6,i = 6
// 5. res = 5 + 6 = 11
,operator ++(i, 0 // 6.评估第二个表达式:res = 6,i = 7
)< span class =code-comment> //
7. res = 11 + 6 = 17



B)看到序列号被洗牌,以便首先评估第二个

 result = operator +  //   1.调用 
(运算符+ // 3.评估第一个表达式...
(operator ++(i) // 5.评估第一个表达式:res = 5,i = 5
,operator ++(i) // 4.评估第二个表达式:res = 4,i = 4
// 6. res = 4 + 5 = 9
,operator ++(i, 0 // 2. eval第二个表达式:res = 4,i = 3
// 7. res = 9 + 4 = 13



谨慎的结论是:不要做这种表达。

如果有的话表达式中的ed变量有副作用,它应该是整个表达式中该变量的唯一实例,否则您可能会遇到不希望的效果。



干杯

Andi



PS:如果您对该标准感兴趣,您可能会看到open-std.org:编程语言C ++标准工作草案 [< a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdftarget =_ blanktitle =New Window> ^ ] ,第1.9节:程序执行,第13段及以下(特别是第15段和相关示例)。

[...]与不同参数表达式相关的值计算和副作用未被排序[ ......] - 其中未经检测大致意味着可以按任何顺序对它们进行评估。


consider i=4
and I want to print the result for: ++i + ++i + i++
what would be the result and how?

解决方案

Well, dude, this is your homework. The answer is 17, if you have a compiler around, you can simply calculate it, just with copy-paste. But I won't tell you why. I suggest you consult the precedence table[^], and imagine how the left to right expression tree would look like... and you will have it.


The mean answer is: depends
The problem is that the sequence of evaluating function arguments is undefined. I.e. one compiler might do it this way, another the other way. It might even change with the argument type or the number of argument.

To illustrate this: in C++ the operator can be written as function calls, e.g.:

int result = operator+(operator+(operator++(i), operator++(i)), operator++(i, 0));

Note that operator++(i) is the pre-increment operator, while operator++(i, 0) is the post-increment operator.

Try the two cases:
A) your compiler evaluates the arguments from left to right
B) your compiler evaluates the arguments from right to left

A) see the sequence from left to right (top-down)

result = operator+            // 1. call
         ( operator+          // 2. eval the 1st expression...
             ( operator++(i)  // 3. eval the 1st expression: res = 5,         i = 5
             , operator++(i)  // 4. eval the 2nd expression: res = 6,         i = 6
             )                // 5.                          res = 5 + 6 = 11
          , operator++(i, 0)  // 6. eval the 2nd expression: res = 6,         i = 7
          )                   // 7.                          res = 11 + 6 = 17


B) see the sequence numbers are shuffled so that the 2nd is evaluated first

result = operator+            // 1. call
         ( operator+          // 3. eval the 1st expression...
             ( operator++(i)  // 5. eval the 1st expression: res = 5,         i = 5
             , operator++(i)  // 4. eval the 2nd expression: res = 4,         i = 4
             )                // 6.                          res = 4 + 5 = 9
          , operator++(i, 0)  // 2. eval the 2nd expression: res = 4,         i = 3
          )                   // 7.                          res = 9 + 4 = 13


The prudent conclusion is: don't do this kind of expression.
If any used variable in an expression has a side effect, it should be the only instance of that variable in the whole expression, otherwise you might experience undesired effects.

Cheers
Andi

PS: If you are interested in the standard, you might for example see open-std.org: Working Draft, Standard for Programming Language C++[^], section 1.9: Program execution, paragraph 13 and following (especially paragraph 15 and associated examples).
"[...]Value computations and side effects associated with different argument expressions are unsequenced[...]" - where "unsequenced" roughly means that they can be evaluated in any order.


这篇关于如何在语句中评估涉及postfix和prefix运算符的表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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