表达式中的多个后缀增量运算符[C ++] [英] Multiple postfix increment operator in an expression[C++]

查看:133
本文介绍了表达式中的多个后缀增量运算符[C ++]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

任何人都可以解释这些未定义的行为(i = i ++ + ++ i,i = i ++,etc…)

我现在有我的讲师班,我的讲师提到一个表达式如下:

I'm having my lecturer class right now and my lecturer mention an expression as follows:

int a , b;
a = 4;
b = a++ + a--;



问题



我的讲师说表达式值可以被定义为:它是8.这意味着一个值4被加和并且在它递增和递减之前被分配给b。

The Question

My lecturer says this expression value can be defined: that it is 8. Which means both a value 4 is summed and assign to b before it's incremented and decremented.

但是对我来说这个表达式的答案是有点模糊,结果将基于编译器实现。这是因为对我来说,编译器可能会先使用a ++部分,即使用值4并将a增加到5,然后表达式为4 + 5 = 9,并分配给b,那么只有a被递减。

But to me, I think this expression answer is kinda vague and the result will be based on the compiler implementation. That's because to me, the compiler might do the a++ part first—that is, use the value 4 and incremented the a to 5, after that the expression is 4 + 5 = 9 and is assigned to b then only the a is decremented.

它也可以首先使用值4并减少1到3,然后将值4与3相加,并分配值到b,然后只将a增加到4。

It might also do the a-- part first by using the value 4 and decremented it by 1 to 3, and later on sum the value 4 with 3 and assign the value to b then only incremented the a back to 4.

我的问题是谁是正确的,我的讲师还是我?

My question is who's correct, my lecturer or me? Because I don't wanna get confused with it and later on will be a problem for me.

推荐答案

你的讲师是错误的。写入相同的变量两次而没有插入序列点是未定义的行为。从规范 J。 2未定义行为::

Your lecturer is wrong. Writing to the same variable twice without an intervening sequence point is undefined behaviour. From the spec, J.2 Undefined behaviour:


在两个序列点之间,一个对象被修改多次,是读取非确定要存储的值(6.5)。

Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored (6.5).

引用是 6.5表达式,第5段:


在上一个和下一个序列点之间,一个对象的存储值最多修改一次,表达。此外,前面的值应该只读,以确定要存储的值。

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

C规范的附录C有一个方便的列表的所有序列点:

Annex C of the C spec has a handy list of all of the sequence points:


以下是5.1.2.3中描述的序列点:

The following are the sequence points described in 5.1.2.3:


  • 在对参数进行求值(6.5.2.2)之后调用函数。

  • 结束第一个操作数以下运算符:逻辑AND &&& (6.5.13);逻辑OR || (6.5.14);条件 (6.5.15);逗号 (6.5.17)。

  • 完整声明符的结尾:declarators 5);

  • 完整表达式的结尾:初始化程序(6.7.8);表达式语句(6.8.3)中的表达式;选择语句的控制表达式( if 切换 )(6.8.4);控制表达式 do 6.8.5);每个 for 语句(6.8.5.3)的表达式;在 return 语句(6.8.6.4)中的表达式。



  • 在每个格式化的输入/输出函数转换说明符(7.19.6,7.24.2)紧接在每次调用比较函数之前和之后,以及任何对比较函数的调用和作为参数传递到该调用的对象的任何移动(7.20.5)之间。

  • The call to a function, after the arguments have been evaluated (6.5.2.2).
  • The end of the first operand of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
  • The end of a full declarator: declarators (6.7.5);
  • The end of a full expression: an initializer (6.7.8); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the expressions of a for statement (6.8.5.3); the expression in a return statement (6.8.6.4).
  • Immediately before a library function returns (7.1.4).
  • After the actions associated with each formatted input/output function conversion specifier (7.19.6, 7.24.2).
  • Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.20.5).

这篇关于表达式中的多个后缀增量运算符[C ++]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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