这是C ++标准中的歧义吗? [英] Is this an ambiguity in the C++ standard?

查看:57
本文介绍了这是C ++标准中的歧义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授声称这段代码:

int f()

{

int x = 5;

int& y = x;

x + = ++ y;


返回x;

}


可以返回11(如果y移入寄存器,递增,x是

移入单独的寄存器,则执行x = x + y,结果在

x = 5 + 6 = 11)或12中我们所期望的(因为x和y指的是相同的

位置,编译器应该只将它移入寄存器一次

导致x = 5 + 1,然后x = 6 + 6 = 12)。


现在为了这个问题(对它来说)永远可能是11),

编译器必须是脑死亡。但是,有没有人知道这是否实际上违反了C ++标准以任何方式实现编译器使得f()导致11而不是12?并且任何人都可以指出标准中的相关部分吗?


我不是在寻找一个可能永远不会发生的探索
练习(我已经知道了)。我真的必须要知道这是否符合C ++标准,因为这是我在这个问题上能够反驳

积分的唯一方法。


谢谢!

粘土

My professor claims that this code:
int f()
{
int x = 5;
int &y = x;
x += ++y;

return x;
}

Can return either 11 (if y is moved into a register, incremented, x is
moved into a seperate register, then x=x+y is performed, resulting in
x=5+6=11) or 12 as we would expect (since x and y refer to the same
location the compiler should only move it into a register once
resulting in x=5+1, then x=6+6=12).

Now in order for this to be a problem (for it to ever possibly be 11),
the compiler would have to be braindead. However, does anyone know if
this actually goes against the C++ standards in any way to implement
the compiler such that f() results in 11 instead of 12? And can anyone
point out that relevant section in the standards?

I''m not looking for an explination that this could never happen in
practice (I already know this). I really have to find out if this goes
against the C++ standards, because it''s the only way I can argue back
points on this issue.

Thanks!
Clay

推荐答案

< Cl ** *******@yahoo.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...
<Cl*********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
我的教授声称这段代码:
int f()
{
int x = 5;
int& y = x;
x + = ++ y;

返回x;
}
可以返回11(如果y移入寄存器,递增,x是
移入一个单独的寄存器,然后执行x = x + y,得到
x = 5 + 6 = 11)或12,正如我们所期望的那样(因为x和y指的是相同的位置,编译器只应将其移入寄存器一次
导致x = 5 + 1,然后x = 6 + 6 = 12)。
My professor claims that this code:
int f()
{
int x = 5;
int &y = x;
x += ++y;

return x;
}

Can return either 11 (if y is moved into a register, incremented, x is
moved into a seperate register, then x=x+y is performed, resulting in
x=5+6=11) or 12 as we would expect (since x and y refer to the same
location the compiler should only move it into a register once
resulting in x=5+1, then x=6+6=12).




或其他任何东西可能会发生。


无论是否有寄存器,编译器可能会做什么,

是无关紧要的。行为是未定义的:尝试

不止一次修改一个对象而没有

干预序列点。


我没有标准方便的副本,也许有人

否则可以引用章节和诗句。


-Mike



Or anything else can happen.

What a compiler might do, with or without registers,
is irrelevant. The behavior is undefined: an attempt
is made to modify an object more than once without
an intervening sequence point.

I don''t have my copy of the standard handy, perhaps someone
else can quote chapter and verse.

-Mike


你的帖子给了我google的正确术语。在C或C ++的单个语句中,你不能
多次修改变量。我没有意识到这是标准规定的禁忌,但我现在理解了b $ b。


谢谢。

Your post gave me the correct terminology to google for. You can''t
modify a variable more than once in a single statement in C or C++. I
didn''t realize that was a no-no specified by the standard, but I
understand now.

Thanks.




Mike Wahler写道:

Mike Wahler wrote:
我不是有我的标准方便的副本,也许是其他人可以引用章和节。
I don''t have my copy of the standard handy, perhaps someone
else can quote chapter and verse.




标准的5/4:


< QUOTE>

除非另有说明,否则个别

运算符和子表达式的操作数的评估顺序

表达式和副作用发生的顺序是

未指定.5)在之前的

和下一个序列点之间,标量对象应具有其存储值<评价

表达式最多修改一次
。此外,只能访问先前价值

来确定要存储的价值。

每个允许值满足本段要求

表示完整

表达式的子表达式;否则行为未定义。 [例如:

i = v [i ++]; //行为未指定

i = 7,i ++,i ++; //我变成了9

i = ++ i + 1; //行为未指定

i = i + 1; // i的值增加

-end example]

< / QUOTE>

并且有用的是:
http://www.open- std.org/jtc1/sc22/wg...fects.html#351


问候,

Sumit。



5/4 of the Standard:

<QUOTE>
Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual
expressions, and the order in which side effects take place, is
unspecified.53) Between the previous
and next sequence point a scalar object shall have its stored value
modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be accessed only
to determine the value to be stored.
The requirements of this paragraph shall be met for each allowable
ordering of the subexpressions of a full
expression; otherwise the behavior is undefined. [Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
-end example]
</QUOTE>
And also helpful is:
http://www.open-std.org/jtc1/sc22/wg...fects.html#351

Regards,
Sumit.


这篇关于这是C ++标准中的歧义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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