对'a'的多次无序修改,a = a++ [英] Multiple unsequenced modifications to 'a',a = a++

查看:47
本文介绍了对'a'的多次无序修改,a = a++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能在 Objective-C 中执行以下操作?

Why i can't do the following in Objective-C?

a = (a < 10) ? (a++) : a;

a = (a++)

推荐答案

为了扩展 Sulthan 的答案,您的表达式有几个问题,至少是简单的赋值(案例 2).

To extend Sulthan's answer, there are several problem with your expressions, at least the simple assignment (case 2).

A.这样做是没有意义的.即使 a++ 有一个可以赋值的值(是一个非空表达式),它也会自动将结果赋值给 a 本身.所以你能期待的最好的相当于

A. There is no sense in doing so. Even a++ has a value (is a non-void expression) that can be assigned, it automatically assigns the result to a itself. So the best you can expect is equivalent to

a++;

作业根本无法改善作业.但这不是错误信息.

The assignment cannot improve the assignment at all. But this is not the error message.

B.Sulthans 替换声明是一个更好的例子.更糟糕的是:++ 运算符的值是 a(在表达式的开头),并且在处增加 a 的效果未来某个时间点:增量可以延迟到下一个序列点.

B. Sulthans replacement of the statement is a better case. It is even worse: The ++ operator has the value of a (at the beginning of the expression) and the effect to increment a at some point in future: The increment can be delayed up to the next sequence point.

更新操作数的存储值的副作用应该发生在前一个和下一个序列点之间.

The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point.

(ISO/IEC 9899:TC3, 6.5.2.4, 2)

(ISO/IEC 9899:TC3, 6.5.2.4, 2)

但是赋值运算符=不是一个序列点(附录C).

But the assignment operator = is not a sequence point (Annex C).

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

The following are the sequence points described in 5.1.2.3:

[列表中既不包含赋值运算符,也不包含 )]

因此该表达可以替换为 Sulthan 所说的:

Therefore the expression can be replaced with what Sulthan said:

int tmp = a; // get the value of a
a = a + 1 //post-increment a
a = tmp // do the assignment

结果,a 仍然包含旧值.

with the result, that a still contains the old value.

或者表达式可以用这个代码代替……:

Or the expression can be replaced with this code …:

int tmp = a; // get the value of a
a = tmp // assignemnt
a = a + 1 // increment

... 具有不同的结果(a 递增).这是错误消息所说的:没有定义的序列(必须应用操作的顺序.)

… with a different result (a is incremented). This is what the error message says: There is no defined sequence (order the operations has to be applied.)

您可以使用逗号运算符(它的主要用途是什么)插入序列点,...

You can insert a sequence point using the comma operator , (what is the primary use of it), …

a++, a=a; // first increment, then assign

……但这显示了你想要做的事情的全部意义.

… but this shows the whole leak of meaning of what you want to do.

这与您的第一个示例相同.虽然 ? 是一个序列点……:

It is the same with your first example. Though ? is a sequence point …:

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

The following are the sequence points described in 5.1.2.3:

... 以下运算符的第一个操作数的结尾: [...] 条件式 ?(6.5.15);[…].

… The end of the first operand of the following operators: […] conditional ? (6.5.15);[…].

... 增量 (a++) 和赋值 (a=) 都在 ? 运算符被求值之后,因此是无序的 ("以随机顺序")再次.

… both the increment (a++) and the assignment (a=) are after the ? operator is evaluated and therefore unsequenced ("in random order") again.

使评论小心";更具体:不要在表达式中两次使用递增的对象.(除非有明确的序列点).

To make the comment "Be careful" more concrete: Don't use an incremented object in an expression twice. (Unless there is a clear sequence point).

int a = 1;
… = a++ * a;

... 评估什么?2?1?未定义,因为可以在读取a第二次"后进行增量.

… evaluates to what? 2? 1? Undefined, because the increment can take place after reading a "the second time".

顺便说一句:Q 与 Objective-C 无关,而是与纯 C 相关.在这一点上,Objective-C 对 C 没有影响.我更改了标签.

BTW: The Q is not related to Objective-C, but to pure C. There is no Objective-C influence to C in that point. I changed the tagging.

这篇关于对'a'的多次无序修改,a = a++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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