C / C ++评估顺序的模糊性 [英] C/C++ Ambiguity in Order of Evaluation

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

问题描述

你好,


我刚刚加入,这是我的第一篇文章。


我从来没有能够解决这个问题

C / C ++中的评估顺序以及操作符优先级的相关问题,使用

括号。

Hello,

I have just joined and this is my first post.

I have never been able to resolve the issue of order of evaluation in
C/C++ and the related issue of precedence of operators, use of
parentheses.


>从参考手册我有: -
>From the reference manual I have :-



1)子表达式的评估顺序由

决定运算符的优先级和分组。

2)运算符操作数的评估顺序未定义,除非

注明。


我刚刚在另一个论坛上被告知

v = u - ((u& - (u = 1))<< 1);

有未定义的行为和给出的理由是

操作数的评估顺序u, - (u = 1)&未定义。

一元 - 具有最高优先级或使用括号 -

(u = 1)无关紧要。

同样在互联网上,我得到的印象是

x = a * b + c;

是评价的顺序是a * b, ? + c,x =?

但是评估的顺序(未定义)可能是(c),a * b,

x =? +?


同一个人似乎毫不含糊地说(?)优先级

的运营商与评估顺序无关,但只是

关于将操作数绑定到运算符。


所以我有以下问题: -

1)v = u - ((u& - (u = 1))<< 1);这是未定义的,为什么?

2)运营商的优先级是否决定了评估顺序?如果

是,那么在什么情况下。

3)如果对2)的答案是否定的,那么如果优先于

,C将如何受到影响运算符以这种方式重新定义: -

运算符的优先级确定操作数的分组与

运算符 - 没有参考评估顺序。


最好的问候,

Rasjid

1) "The order of evaluation of subexpressions is determined by the
precedence and grouping of operators."
2) order of evaluation of operands of operators undefined except where
noted.

I was just told at another forum that
v = u - ((u & -(u = 1)) << 1);
has undefined behavior and the reason given was
the order of evaluation of operands u, -(u = 1) of & is undefined.
That unary - has the highest precedence or the use of parentheses in -
(u = 1) is of no consequence.

Also all over the internet, the impression I get about
x = a * b + c;
is that the order of evaluation is a * b, ? + c, x = ?
But the order(undefined) of evaluation may be (c) , a * b,
x = ? + ?

The same person seem to say unequivocally (?) that precedence
of operators has nothing to do with order of evaluation, but only
about binding operands to operators.

So I have the following questions :-
1) v = u - ((u & -(u = 1)) << 1); Is this undefined and why?
2) Does precedence of operators determine order of evaluation ? If
Yes, then in what situations.
3) If answer to 2) is No, then how is C affected if precedence of
operators is redefined in sort of this manner :-
"precedence of operators determines the grouping of operands with
operators" - without any reference to order of evaluation.

Best Regards,
Rasjid

推荐答案

Rasjid说:
Rasjid said:

您好,


我刚刚加入,这是我的第一个帖子。
Hello,

I have just joined and this is my first post.



欢迎来到comp.lang.c.

Welcome to comp.lang.c.


>

我从来没有能够解决评估顺序问题

C / C ++
>
I have never been able to resolve the issue of order of evaluation in
C/C++



我们在这里讨论的语言称为C.在comp.lang.c ++中讨论了C ++语言

。你在这里学到的关于C的信息可能,或者可能不是b $ b,也适用于C ++。

The language we discuss here is called C. The C++ language is discussed
in comp.lang.c++. Information about C that you learn here may, or may
not, apply to C++ as well.


以及相关的优先权问题运营商,使用

括号。
and the related issue of precedence of operators, use of
parentheses.



优先级与评估顺序几乎没有任何关系。

Precedence has little or nothing to do with order of evaluation.


>>从我的参考手册中: -
>>From the reference manual I have :-



1)子表达式的评估顺序由
$ b决定$ b优先级和运算符分组。

1) "The order of evaluation of subexpressions is determined by the
precedence and grouping of operators."



这是假的。例如,考虑:


#include< stdio.h>


int f(void){printf("嗨!I) '' MF()\\\
");返回2; }

int g(void){printf("嗨!我是g()\ n");返回3; }

int h(void){printf(" Hi!I''h h()\ n");返回4; }


int main(无效)

{

printf("结果是%d。\ n", f()+ g()* h());

返回0;

}


如果你的参考手册是正确,g()和h()将在f()之前评估

。但在我的系统上,输出是:


嗨!我是f()

嗨!我是g()

嗨!我是()

结果是14.


表示首先评估f()。

That''s false. For example, consider:

#include <stdio.h>

int f(void) { printf("Hi! I''m f()\n"); return 2; }
int g(void) { printf("Hi! I''m g()\n"); return 3; }
int h(void) { printf("Hi! I''m h()\n"); return 4; }

int main(void)
{
printf("The result is %d.\n", f() + g() * h());
return 0;
}

If your reference manual were correct, g() and h() would be evaluated
before f(). But on my system, the output is:

Hi! I''m f()
Hi! I''m g()
Hi! I''m h()
The result is 14.

which suggests that f() is evaluated first.


2)运算符操作数的评估顺序未定义,除非

注明。
2) order of evaluation of operands of operators undefined except where
noted.



事实上,评估的顺序是未指定的而不是未定义的。

这意味着/是/评估的顺序,但是该命令完全取决于实施。

In fact, the order of evaluation is unspecified rather than undefined.
That means that there /is/ an order of evaluation, but that order is
entirely up to the implementation.


我刚在另一个论坛上被告知

v = u - ((u& - (u = 1))<< 1);

有未定义的行为,给出的理由是

评估顺序操作数u, - (u = 1)&未定义。

一元 - 具有最高优先级或使用括号 -

(u = 1)无关紧要。
I was just told at another forum that
v = u - ((u & -(u = 1)) << 1);
has undefined behavior and the reason given was
the order of evaluation of operands u, -(u = 1) of & is undefined.
That unary - has the highest precedence or the use of parentheses in -
(u = 1) is of no consequence.



表达式未定义的原因是它违反了C标准的shall

子句,它不是约束:


"在上一个和下一个序列点之间,一个对象应该通过表达式的评估最多修改一次它的

存储值。

此外,只能访问先前的值以确定要存储的

值。

The reason the expression is undefined is that it violates a "shall"
clause of the C Standard that is not a constraint:

"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 accessed only to determine the
value to be stored."


全部在互联网上,我得到的印象

x = a * b + c;

是评价的顺序是* b,? + c,x =?
Also all over the internet, the impression I get about
x = a * b + c;
is that the order of evaluation is a * b, ? + c, x = ?



不。这取决于实现,在这方面,实现可以并确实有所不同。

Nope. It''s up to the implementation, and implementations can and do vary
in this respect.


但订单(未定义)评估可能是(c),a * b,

x =? +?


同一个人似乎毫不含糊地说(?)优先级

的运营商与评估顺序无关,但只是

关于将操作数绑定到运算符。
But the order(undefined) of evaluation may be (c) , a * b,
x = ? + ?

The same person seem to say unequivocally (?) that precedence
of operators has nothing to do with order of evaluation, but only
about binding operands to operators.



绝对正确。唯一的联系是,在某些情况下,给定

优先级将使得几乎不可能找到一个以上的订单

的评估是有道理的,但这并不重要至于程序员所关注的
。请参阅我的f()g()h()示例(上图)。

唯一安全的假设是,在优先级(或实际上是关联性)和评估之间没有任何关联



order。

Absolutely right. The only connection is that in some cases a given
precedence will make it almost impossible to find more than one order
of evaluation that makes sense, but this is of no consequence as far as
the programmer is concerned. See my f() g() h() example (above). The
only safe assumption you can make is that there is no connection
whatsoever between precedence (or indeed associativity) and evaluation
order.


>

所以我有以下问题: -

1)v = u - ((u & - (u = 1))<< 1);这是不确定的,为什么?
>
So I have the following questions :-
1) v = u - ((u & -(u = 1)) << 1); Is this undefined and why?



是的,它是未定义的,因为它违反了应该。这不在

约束范围内。 (如果它在一个约束内,它仍然是未定义的
,但它也需要一条诊断消息。)

Yes, it''s undefined, because it violates a "shall" that is not within a
constraint. (If it were within a constraint, it would still be
undefined, but it would also require a diagnostic message.)


2)运营商的优先顺序是否决定评估顺序?如果

是的,那么在什么情况下。
2) Does precedence of operators determine order of evaluation ? If
Yes, then in what situations.



不,它不会,除非意外。例如,很难出现

的x * y + z评估顺序,它没有做

乘法之前加成。尽管如此,即使在那里,

正如我之前的例子所示,操作数的评估顺序

是任何人的猜测。

No, it doesn''t, except accidentally. For example, it''s hard to come up
with an order of evaluation for x * y + z that doesn''t do the
multiplication before it does the addition. Nevertheless, even there,
as my earlier example showed, the order of evaluation of the operands
is anyone''s guess.


3)如果对2)的答案为否,那么如果以这种方式重新定义

运算符的优先级,C将如何受到影响: -

运算符的优先级决定了操作数的分组与

运算符 - 未提及评估顺序。
3) If answer to 2) is No, then how is C affected if precedence of
operators is redefined in sort of this manner :-
"precedence of operators determines the grouping of operands with
operators" - without any reference to order of evaluation.



这根本不会影响C,因为它已经像那样工作了。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

This affects C not at all, since it already works like that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Rasjid< sn ******** @ yahoo.com.sgwrote:
Rasjid <sn********@yahoo.com.sgwrote:

我从来没有能够解决评估顺序问题

C / C ++
I have never been able to resolve the issue of order of evaluation in
C/C++



没有C / C ++这样的东西。有C,有C ++,在一些

区域它们是相同的,有些它们显然是不同的,并且在

中有些他们是微妙但显着不同的。我不知道你的问题属于哪个部分。我会回答C.

There''s no such thing as C/C++. There''s C, and there''s C++, and in some
areas they are the same, in some they''re obviously different, and in
some they''re subtly but significantly different. I have no idea which
part your question falls in; I''ll answer it for C.


以及操作符优先级的相关问题,使用括号。
and the related issue of precedence of operators, use of parentheses.

从参考手册我有: -
From the reference manual I have :-



1)子表达式的评估顺序由

决定运营商的优先顺序和分组。

1) "The order of evaluation of subexpressions is determined by the
precedence and grouping of operators."



这不是真的。

This is not true.


2)运算符操作数的评估顺序undefined除了

注意到。
2) order of evaluation of operands of operators undefined except where
noted.



这是真的。

This is true.


我刚在另一个论坛上被告知

v = u - ((u& - (u = 1))<< 1);

有未定义的行为,给出的理由是

订单评估操作数u, - (u = 1)&未定义。
I was just told at another forum that
v = u - ((u & -(u = 1)) << 1);
has undefined behavior and the reason given was
the order of evaluation of operands u, -(u = 1) of & is undefined.



它确实有未定义的行为,但评估的顺序是

与它无关。你正在那个表达式中修改你(在

子表达式u = 1中),并且你也使用u的值来表示

的原因并不直接需要那个修改(另外两个你的)。

这样做可以调用UB。如果你修改任何对象,那么所有其他对

的引用,两个周围序列点之间的相同对象必须是确定新值的目的。


It does have undefined behaviour, but the order of evaluation has
nothing to do with it. You are modifying u in that expression (in the
sub-expression u = 1), and you are also using the value of u for a
reason not directly necessary for that modification (the other two u''s).
Doing so invokes UB. If you modify any object, all other references to
that same object between the two surrounding sequence points must be for
the purpose of determining the new value.


同样在互联网上,我得到的印象是

x = a * b + c;

就是订单评价是* b,? + c,x =?
Also all over the internet, the impression I get about
x = a * b + c;
is that the order of evaluation is a * b, ? + c, x = ?



错误。或者更确切地说,不保证。

Wrong. Or rather, not guaranteed.


但是评估的顺序(未定义)可能是(c),a * b,

x =? +?
But the order(undefined) of evaluation may be (c) , a * b,
x = ? + ?



可能是。或者它可能是b,c,a,乘法,加法。

May be. Or it may be b, c, a, multiplication, addition.


同一个人似乎明确地说(?)优先级

运算符与评估顺序无关,但只有

关于将操作数绑定到运算符。
The same person seem to say unequivocally (?) that precedence
of operators has nothing to do with order of evaluation, but only
about binding operands to operators.



是的,这是真的。

Yes, that is true.


所以我有以下问题: -

1)v = u - ((u& - (u = 1))<< 1);这是不确定的,为什么?
So I have the following questions :-
1) v = u - ((u & -(u = 1)) << 1); Is this undefined and why?



未定义,但原因不同;见上文。

Undefined, but for a different reason; see above.


2)运营商的优先级是否决定评估顺序?
2) Does precedence of operators determine order of evaluation ?



编号除了一些特殊操作符:&&,||,?:和逗号。


实际上,C运营商并没有优先权。它们相对于其他运算符的绑定程度有多强,以及在哪个方向上由语法决定,而不是由优先级表决定。结果是

相同,但C中子表达式的评估顺序仅为

由两件事决定:

1很明显,子表达式必须在它们包含

更大的表达式之前进行评估(但不一定要在该表达式或任何其他更大表达式的任何其他子表达式之前); < b / c
2. C程序中的某些点是序列点,所有表达式之前的所有表达式

都要在所有表达式之后进行评估

之后。序列点包括表达式语句结束

和函数的调用,还有上面的运算符。

No. Except for a few special operators: &&, ||, ?: and comma.

Actually, C operators don''t have precedence as such. How strongly they
bind relative to which other operators, and in which direction, is
determined by the grammar, not by a precedence table. The result is the
same, though - the order of evaluation of sub-expressions in C is only
determined by two things:
1. obviously, sub-expressions must be evaluated before their containing
larger expressions (but not necessarily before any other sub-
expression of that or any other larger expression);
2. some points in a C program are sequence points, and all expressions
before a sequence point shall be evaluated before all expressions
after it. Sequence points include the end of an expression statement
and the call of a function, but also the operators above.


3)如果回答到2)是否,那么如果以这种方式重新定义

运算符的优先级,C如何受到影响: -

"运算符的优先级决定了操作数的分组与

运营商 - 未提及评估顺序。
3) If answer to 2) is No, then how is C affected if precedence of
operators is redefined in sort of this manner :-
"precedence of operators determines the grouping of operands with
operators" - without any reference to order of evaluation.



解释分组。


Richard

Explain "grouping".

Richard




Rasjid < sn ******** @ yahoo.com.sgha scritto nel messaggio新闻:11 ********************** @ x35g2000prf.googlegr oups .com ...

"Rasjid" <sn********@yahoo.com.sgha scritto nel messaggio news:11**********************@x35g2000prf.googlegr oups.com...

您好,


我刚加入,这是我的第一篇文章。


我从来没有能够解决

C / C ++中的评估顺序问题以及运营商优先级的相关问题,使用

括号。
Hello,

I have just joined and this is my first post.

I have never been able to resolve the issue of order of evaluation in
C/C++ and the related issue of precedence of operators, use of
parentheses.

>>从参考手册我有: -
>>From the reference manual I have :-



1) 子表达式的评估顺序由

优先级和运算符分组决定。

2)运算符操作数的评估顺序undefined除了

注意到。


我刚在另一个论坛上被告知

v = u - ((u& - (u = 1) )<< 1;;

有未定义的行为,给出的理由是

操作数的评估顺序u, - (u = 1)&a熔点;未定义。

一元 - 具有最高优先级或使用括号 -

(u = 1)无关紧要。

同样在互联网上,我得到的印象是

x = a * b + c;

是评价的顺序是a * b, ? + c,x =?

但是评估的顺序(未定义)可能是(c),a * b,

x =? +?

1) "The order of evaluation of subexpressions is determined by the
precedence and grouping of operators."
2) order of evaluation of operands of operators undefined except where
noted.

I was just told at another forum that
v = u - ((u & -(u = 1)) << 1);
has undefined behavior and the reason given was
the order of evaluation of operands u, -(u = 1) of & is undefined.
That unary - has the highest precedence or the use of parentheses in -
(u = 1) is of no consequence.

Also all over the internet, the impression I get about
x = a * b + c;
is that the order of evaluation is a * b, ? + c, x = ?
But the order(undefined) of evaluation may be (c) , a * b,
x = ? + ?



x = a * b + c

可以用作


t1 = a; t2 = b; t3 = c; t4 = t1 * t2; t5 = t4 * t3; x = t5;


或对这些进行任何改组。唯一的要求是(当然)

t4在t1和t2之后被分配,t5被分配

在t4和t3之后,并且x在t5之后分配。允许任何其他

重新排序。

x = a * b + c
can work as

t1 = a; t2 = b; t3 = c; t4 = t1 * t2; t5 = t4 * t3; x = t5;

or any reshuffling of these. The only requirement is (of course)
that t4 gets assigned after both t1 and t2 do, t5 gets assigned
after t4 and t3 do, and x is assigned after t5. Any other
reordering is allowed.


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

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