大师:奇怪的C ++运算符行为 [英] to guru : strange C++ operator behaviour
问题描述
我发现C ++中的操作符行为很有趣:
int i = 1;
printf("%d",i ++ + i ++);
我认为表达式i ++ + i ++的值是多少。 _must_是3,但我测试的所有
编译器打印2.
然后我尝试了另一个例子
的std ::矢量< INT> a;
a.push_back(1);
a.push_back(2);
std :: vector< int> :: iterator i = a.begin();
printf("%d",* i ++ + * i ++);
不同的编译器打印不同的值:2或3,认为_must_是3.
可能是因为编译器C的兼容性,因为迭代器
是C ++构造,因此它们的行为适合C ++标准,
而基本类型编译器保证C行为。
Bjarne Stroustrup在他的书中写道
y = + + X;相当于y =(x + = 1);
而
y = x ++;相当于y =(t = x,x + = 1,t);
为什么所有编译器都错误地计算表达式?
非常感谢提前,
Dmitriy Iassenev。
2003年10月2日星期四19: 55:37 +0300,Dmitriy Iassenev
< ia ****** @ gsc-game.kiev.ua>写道:
我发现C ++中的操作符行为很有趣:
int i = 1;
printf("%d",i ++ + i ++);
我认为表达式i ++ + i ++的值是_must_是3,但我测试的所有
编译器都打印2。
这是一个FAQ:
http://www.eskimo.com/~scs/C-faq/ s3.html
Tom
Dmitriy Iassenev写道:hi,
我在C ++中发现了一个有趣的操作符行为:
int i = 1;
printf("%d",i ++ + i ++);
我认为表达式i ++ + i ++的值是多少。 _must_是3,但是我测试的所有编译器都打印2。
你认为错了。以上是未定义的行为。您尝试使用插入序列点多次更改变量的
值。允许
编译器执行任何操作,包括格式化
硬盘。在Google上搜索序列点i +++++ i。一页是:
http://www.embedded。 com / story / OEG20020625S0041
-
WW aka Attila
< blockquote class =post_quotes>
int i = 1;
printf("%d",i ++ + i ++);
我认为表达式的值是 i ++ + i ++ _must_是3,但是我测试的所有
编译器都打印出来2.
Bjarne Stroustrup在他的书中写道:
y = ++ x;相当于y =(x + = 1);
而
y = x ++;相当于y =(t = x,x + = 1,t);
为什么所有编译器都错误地计算表达式?
他的陈述完全同意你的考试,不是吗?你得到2,
这是1 + 1,在计算出该值后,我会增加
(两次)。使用他的符号,
y = x ++ + x ++;就像
y =(t = x + x,x + = 1,x + = 1,t);
对吧?
-Howard
hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
Then I tried another example
std::vector<int> a;
a.push_back(1);
a.push_back(2);
std::vector<int>::iterator i = a.begin();
printf("%d",*i++ + *i++);
Different compilers print different values : 2 or 3, thought it _must_ be 3.
Possibly, it is because of the compilers C compatibility, since iterators
are C++ construction and their behaviour therefore suits C++ standards,
while for the basic types compilers guarantee C behaviour.
Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);
Why do all the compilers incorrectly compute the expressions?
Many thanks in advance,
Dmitriy Iassenev.
On Thu, 2 Oct 2003 19:55:37 +0300, "Dmitriy Iassenev"
<ia******@gsc-game.kiev.ua> wrote:
hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
This is an FAQ:
http://www.eskimo.com/~scs/C-faq/s3.html
Tom
Dmitriy Iassenev wrote:hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all
the compilers I tested print 2.
You think wrong. The above is undefined behavior. You try to change the
value of a variable more than once with an intervening sequence point. The
compiler is allowed to do whatever it wants, including formatting your
harddisk. Search for sequence point i+++++i on Google. One page is:
http://www.embedded.com/story/OEG20020625S0041
--
WW aka Attila
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);
Why do all the compilers incorrectly compute the expressions?
His statement agrees with your test perfectly, doesn''t it? You get back 2,
which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,
y = x++ + x++; would be like
y = (t = x+x, x+=1, x+= 1, t);
Right?
-Howard
这篇关于大师:奇怪的C ++运算符行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!