std :: cout<< x<< " " << x ++<<的std :: ENDL [英] std::cout << x << " " << x++ << std::endl
问题描述
嗨!
有人可以向我解释为什么这个操作不符合我的预期吗?
int main()
{
int x = 2;
std :: cout<< x<< " " << x ++<< std :: endl;
返回0;
}
main.cc:在函数''int main()' ':
警告:'x''上的操作可能未定义
结果是
3 2 >
我希望2 2.
谢谢
Filipe Sousa
Hi!
Could someone explain to me why this operation is not what I was expecting?
int main()
{
int x = 2;
std::cout << x << " " << x++ << std::endl;
return 0;
}
main.cc: In function ''int main()'':
warning: operation on ''x'' may be undefined
The result is
3 2
I would expect 2 2.
Thanks
Filipe Sousa
推荐答案
Filipe Sousa写道:
Filipe Sousa wrote:
嗨!
有人可以向我解释为什么这个操作不符合我的预期吗?
int main()
{
int x = 2;
std :: cout<< x<< " " << x ++<< std :: endl;
返回0;
}
main.cc:在函数''int main()''中:
警告:操作'' x''可能未定义
结果是
3 2
我希望2 2.
谢谢
Filipe Sousa
Hi!
Could someone explain to me why this operation is not what I was expecting?
int main()
{
int x = 2;
std::cout << x << " " << x++ << std::endl;
return 0;
}
main.cc: In function ''int main()'':
warning: operation on ''x'' may be undefined
The result is
3 2
I would expect 2 2.
Thanks
Filipe Sousa
结果未定义为您的示例。这是因为x ++可以编译为在整个语句之后或在使用之后立即增加
,就像在这种情况下一样。简而言之,如果您在使用
operator ++时需要可预测的行为,那么应用它的变量应该只在被评估的表达式中出现一次
。
JB
The result is undefined for your example. This is because x++ could
compiled to increment after the whole statement or immediately after use
as in this case. In short if you want predictable behaviour when using
operator ++ then the variable it is applied to should only appear once
in the expression being evaluated.
JB
Filipe Sousa写道:
Filipe Sousa wrote:
嗨!
可以有人向我解释为什么这个操作不是我想要的?
int main()
{
int x = 2;
标准: :cout<< x<< " " << x ++<<的std :: ENDL;
这相当于:
运营商<< (
运算符<<(
运算符<<(
运算符<<(std :: cout,x),
""
),
x ++
),
std ::结束;
);
在这样的表达式中,子表达式的评估顺序是
实现定义。特别是,无法保证
运算符<<(std :: cout,x)
将在
x ++
返回0;
}
main.cc:在函数''int main()''中:
警告:''x'上的操作可能未定义
结果是
3 2
这只是一种可能结果。
我希望2 2。
Hi!
Could someone explain to me why this operation is not what I was
expecting?
int main()
{
int x = 2;
std::cout << x << " " << x++ << std::endl;
This is equivalent to:
operator<< (
operator<< (
operator<< (
operator<< ( std::cout, x ),
" "
),
x++
),
std::endl;
);
In such an expression, the order of evaluation of subexpressions is
implementation defined. In particular, there is no guarantee that
operator<<( std::cout, x )
will be evaluated before
x++
return 0;
}
main.cc: In function ''int main()'':
warning: operation on ''x'' may be undefined
The result is
3 2
That is just one possible result.
I would expect 2 2.
这是另一个可能的结果。
Best
Kai-Uwe Bux
That is another possible result.
Best
Kai-Uwe Bux
Filipe Sousa sade:
Filipe Sousa sade:
嗨!
有人可以向我解释为什么这个操作不是我所期待的吗?
int main()
{
int x = 2;
std :: cout< ;< x<< " " << x ++<< std :: endl;
返回0;
}
main.cc:在函数''int main()''中:
警告:操作'' x''可能未定义
结果是
3 2
我希望2 2.
谢谢
Filipe Sousa
Hi!
Could someone explain to me why this operation is not what I was expecting?
int main()
{
int x = 2;
std::cout << x << " " << x++ << std::endl;
return 0;
}
main.cc: In function ''int main()'':
warning: operation on ''x'' may be undefined
The result is
3 2
I would expect 2 2.
Thanks
Filipe Sousa
-
TB @ SWEDEN
http://www.parashift.com/c++-faq-lit...html#faq-39.16
--
TB @ SWEDEN
这篇关于std :: cout<< x<< " " << x ++<<的std :: ENDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!