为了简单描述问题,请看下面的代码:
int main()
{
int a=123;
({if (a) a=0;});
return 0;
}
我从 [-Wsequence-point] 收到此警告
Line 4: warning: operation on 'a' may be undefined
我的g++版本是4.4.5
我将不胜感激任何能解释这个简单问题的人。
顺便说一句,你可以在#7中找到我的原始程序和原始问题this http://bbs.csdn.net/topics/390475508中文网站(非必须)
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, b;
({;});
({if (a) b=0;});
return 0;
}
这段代码得到了没有警告!
所以我认为真正的原因是关于序列点的。
请帮忙!
UPD2:
很抱歉@AndyProwl 没有接受他在 UPD1 之前就被接受的答案。按照他的建议,我可能会问一个新问题(UPD1 是一个与原始问题不同的新问题)。我会再次接受他的回答,因为无论如何它肯定会避免警告。:)
如果我决定提出新问题,我将更新此问题以添加链接。