评估顺序 [英] Order of evaluation

查看:80
本文介绍了评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下陈述是否属实?


"在评估算子本身之前,需要对运算符的操作数进行全面评估




我能想到的唯一方法就是实际上可能会违反这个b / b,就像a + b这样的子表达式返回它值

(a + b)用于父表达式,但在将该值分配给a之前将其保留一段时间

。这是允许的吗?


如果上面的陈述是真的,那么定义如下:


int a = / * something * /, b = / *别的东西* /;

a ^ = b ^ = a ^ = b;


此页面上的几个人:
http://c2.com/cgi/wiki?ThreeStarProgrammer ,说上面的

表达式是未定义的,因为它在没有

干预序列点的情况下两次修改''a''。如果在评估运算符之前必须完全评估操作数

,那么这不是未定义的,因为它相当于(a ^ =(b ^ =( a ^ = b))),其中每个具有副作用的
运算符取决于下一个的结果。它是否定义为
未定义?


ps。显然,(a ^ = b)

^ =(a ^ = b)中子表达式的评估顺序是不确定的 - 我当然不会不同意这一点。


pps。如果a和b是用户定义的类型,那么^ =运算符将是一个

函数调用,并且函数调用的参数必须是

之前评估的调用,所以在这种情况下它将被定义。

Is the following statement true?

"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."

The only way I can think of that it would be practically possible to
violate this would be for subexpression like a += b to return its value
(a + b) for use in the parent expression, but leave it until some time
later before it assigned that value to a. Is this allowed?

If this statement above is true, then the following is defined:

int a = /*something*/, b = /*something else*/;
a ^= b ^= a ^= b;

A couple of people at this page:
http://c2.com/cgi/wiki?ThreeStarProgrammer, have said that the above
expression is undefined because it modifies ''a'' twice without an
intervening sequence point. If operands have to be fully evaluated
before the operator can be evaluated, this is not undefined though,
because it is equivalent to (a ^= (b ^= (a ^= b))), in which each
operator with side-effects depends on the result of the next. Is it
undefined or not?

ps. Clearly the order of evaluation of the subexpressions in (a ^= b)
^= (a ^= b) is undefined - I''m certainly not disagreeing with that.

pps. If a and b were of a user-defined type, the ^= operator would be a
function call, and the parameters to a function call have to be
evaluated before the call, so in this case it would be defined.

推荐答案

pu ******* @ blueyonder.co.uk 写道:
以下陈述是否属实?

在评估操作员本身之前,需要对操作员的操作数进行全面评估。


是的。

我能想到的唯一方法是,实际上可能会违反这个,就像子表达式一样+ = b返回它的值
(a + b)以便在父表达式中使用,但是稍后将它保留到
之后才会将该值赋给a。这是允许的吗?


是的,如果'a''在

下一个序列点之前改变两次,它有时会造成麻烦。

如果上面的陈述为真,那么定义如下:

int a = / * something * /,b = / *其他东西* /;
a ^ = b ^ = a ^ = b;


不。这个表达式在

序列点之间两次更改''a''和'b''的值。

此页面上有几个人:
http://c2.com/cgi/wiki?ThreeStarProgrammer ,说上面的
表达式是未定义的,因为它在没有
干预序列点的情况下两次修改''a''。如果在评估运算符之前必须完全评估操作数,那么这并不是未定义的,因为它等价于(a ^ =(b ^ =(a ^ = b))),其中每个具有副作用的操作员都取决于下一个的结果。它是不确定的?


未定义。评估操作数(获得值)和侧面

效果是两个不同的事情。

ps。很明显,(a ^ = b)
^ =(a ^ = b)中子表达式的评估顺序是不确定的 - 我当然不会不同意这一点。

pps。如果a和b是用户定义的类型,则^ =运算符将是函数调用,并且在调用之前必须评估函数调用的参数,因此在这种情况下它将被定义。
Is the following statement true?

"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."
Yes.
The only way I can think of that it would be practically possible to
violate this would be for subexpression like a += b to return its value
(a + b) for use in the parent expression, but leave it until some time
later before it assigned that value to a. Is this allowed?
Yes, and it sometimes causes trouble if ''a'' is changed twice before the
next sequence point.
If this statement above is true, then the following is defined:

int a = /*something*/, b = /*something else*/;
a ^= b ^= a ^= b;
Nope. This expression changes the value of ''a'' and ''b'' twice between the
sequence points.
A couple of people at this page:
http://c2.com/cgi/wiki?ThreeStarProgrammer, have said that the above
expression is undefined because it modifies ''a'' twice without an
intervening sequence point. If operands have to be fully evaluated
before the operator can be evaluated, this is not undefined though,
because it is equivalent to (a ^= (b ^= (a ^= b))), in which each
operator with side-effects depends on the result of the next. Is it
undefined or not?
It is undefined. Evaluation of operands (obtaining the values) and side
effects taking place are two different things.
ps. Clearly the order of evaluation of the subexpressions in (a ^= b)
^= (a ^= b) is undefined - I''m certainly not disagreeing with that.

pps. If a and b were of a user-defined type, the ^= operator would be a
function call, and the parameters to a function call have to be
evaluated before the call, so in this case it would be defined.




是的。


V



Yes.

V


> ...评估操作数(获取值)和侧面
> ... Evaluation of operands (obtaining the values) and side
发生的效果是两回事。




这一切都清楚了我现在非常感谢你的帮助。



It all comes clear to me now. Thanks very much for your help.


只是为了确保我有这句话:

int a = 1 ,b = 2,c = 3;

(a = b)= c;

这也是未定义的,不是吗? ''a''可能最终包含2

或3,因为两个作业之间没有序列点。


谢谢,

Tom

Just to make sure I''ve got this straight:
int a = 1, b = 2, c = 3;
(a = b) = c;
This is undefined too, isn''t it? ''a'' could end up containing either 2
or 3 as there are no sequence points between the two assignments.

Thanks,
Tom


这篇关于评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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