后增量和后减量的模糊性 [英] ambiguity of post-increment and post-decrement

查看:79
本文介绍了后增量和后减量的模糊性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读实用C ++编程。昨天,它提到

,后增量和后减量的执行顺序

运营商是模棱两可的。

我以前学过一个后增量或后减量

运算符在整个语句执行完毕后修改操作数,而不是在执行语句期间修改操作数,所以这让我很困惑。 br />

用于说明含糊之处的例子是:

a [i] = i ++; //可以在评估[i]之前或之后增加i。


我用VC6编译器和gnu编译器测试过,但是用相同的

结果:在整个语句执行完毕后发生后减量,而不是在执行期间。


我现在被告知实际关于执行顺序,ANSI C ++标准是不明确的

(无论在与不同编译器制造商进行编译的过程中发生了什么)。


I我仍然在通过C ++工作的过程中,并且我没有b $ b有官方标准的副本,所以我希望更多的学习

个人会能够为我解释这个棘手的问题。

Pracitcal C ++ Programming建议不要使用增量

并在声明中减少运算符,但我已经学会了真的

享受它们。


感谢您的帮助

-mark

I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.

I tested with the VC6 compiler and the gnu compiler, but with the same
results: the post-decrement occurs after the entire statement is
executed, not during it''s execution.

I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).

I am still in the process of working my way through C++ and I don''t
have a copy of the official standard, so I was hoping a more learned
individual would be able to elucidate this troublesome issue for me.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I''ve learned to really
enjoy them.

Thanks for the help
-mark

推荐答案

Mark Turney写道:
Mark Turney wrote:
我是阅读实用C ++编程昨天,它提到了后增量和后减量运算符的执行顺序是模棱两可的。
我之前已经知道后增量或后减量运算符在整个语句执行后修改操作数,而不是在执行语句时修改操作数,所以这让我很困惑。

用于说明歧义的例子是:
a [i] = i ++; //可以在评估[i]之前或之后递增i。

我使用VC6编译器和gnu编译器进行了测试,但结果相同:后递减发生在整个声明是执行的,而不是在执行期间。

我现在被告知实际的ANSI C ++标准关于执行顺序是模棱两可的(无论是什么在与不同编译器制造商进行编译期间发生。


不完全含糊不清,更像是未定义。标准没有指示编译器应该做什么,因此你不能依赖于代码可移植性的特定行为。


//这段代码100%便携,可能同样快......

我仍然在通过C ++工作,我没有官方标准的副本,所以我希望更多的学习<个人能够为我解释这个棘手的问题。
Pracitcal C ++ Programming" Pracitcal C ++ Programming"建议不要在语句中使用增量
和减少运算符,但我已经学会了真正地享受它们。
I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.

I tested with the VC6 compiler and the gnu compiler, but with the same
results: the post-decrement occurs after the entire statement is
executed, not during it''s execution.

I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).
Not quite "ambiguous", more like "undefined". The standard does not
specify what the compiler should do hence you can''t depend on a
particular behaviour for code portability.

// this code is 100% portable and likely just as fast
a[i] = i; ++ i;

I am still in the process of working my way through C++ and I don''t
have a copy of the official standard, so I was hoping a more learned
individual would be able to elucidate this troublesome issue for me.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I''ve learned to really
enjoy them.




查找引用对于序列点。这些是所有

副作用应该完成的点。 ;在一个

表达式的结尾通常是个好看的地方。



look up references for "sequence points". These are points where all
the side effects should be complete. The ";" at the end of an
expression is usually a good place to look.


Mark Turney写道:
Mark Turney wrote:

我正在阅读实用C ++编程。昨天,它提到了后增量和后减量运算符的执行顺序是模棱两可的。
我之前已经知道后增量或后减量运算符在整个语句执行后修改操作数,而不是在执行语句时修改操作数,所以这让我很困惑。

用于说明歧义的例子是:
a [i] = i ++; //可以在评估[i]之前或之后递增i。

我使用VC6编译器和gnu编译器进行了测试,但结果相同:后递减发生在整个声明是执行的,而不是在执行期间。

我现在被告知实际的ANSI C ++标准关于执行顺序是模棱两可的(无论是什么在与不同编译器制造商进行编译期间发生。


Ambigous是错误的词。实际发生增量

时,它就是未定义的。所有你知道的是当达到下一个序列点

时(在上面的例子中这是语句的结尾,'';'')

增量已经发生了。但是当确切地未定义时。

" Pracitcal C ++ Programming"建议不要在语句中使用增量
和减少运算符,但我已经学会了真正地享受它们。

I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.

I tested with the VC6 compiler and the gnu compiler, but with the same
results: the post-decrement occurs after the entire statement is
executed, not during it''s execution.

I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).
Ambigous is the wrong word. It is simply undefined when the increment
actually happens. All you know is that when the next sequence point
is reached (in the above case this is the end of the statement, the '';'')
the increment has happend. But when exactly is undefined.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I''ve learned to really
enjoy them.




什么都没有他们错了。你的程序一定不能依赖于某些特定执行订单的
,这就是全部。

-

Karl Heinz Buchegger kb******@gascad.at




" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息新闻中写道:3F *************** @ gascad.at ...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:3F***************@gascad.at...
Ambigous是错误的词。当增量实际发生时,它就是未定义的。
Ambigous is the wrong word. It is simply undefined when the increment
actually happens.




它只是简单地未定义会发生什么。



It''s just simply undefined what happens.


这篇关于后增量和后减量的模糊性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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