小编FLa*_*S39的帖子

为什么我在C++中的Statement Expression中得到"操作可能未定义"?

简单描述问题,请看下面的代码:

int main()
{
    int a=123;
    ({if (a) a=0;});
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到了[-Wsequence-point]的警告

Line 4: warning: operation on 'a' may be undefined
Run Code Online (Sandbox Code Playgroud)

我的g ++版本是4.4.5

无论谁解释这个简单的问题,我都会感激不尽.

顺便说一句,你可以在这个中文网站的#7中找到我的原始程序和原始问题(不是必需的)

UPD1:

虽然改变代码({if(a) a=0; a;})可以避免警告,但我认识到问题的真正原因可能不是The last thing in the compound statement should be an expression followed by a semicolon.

因为纪录片还说If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.

一个例子可以显示它:

int main()
{
    int a=123, …
Run Code Online (Sandbox Code Playgroud)

c++ expression sequence-points

14
推荐指数
1
解决办法
4562
查看次数

标签 统计

c++ ×1

expression ×1

sequence-points ×1