Arm*_*yan 6 c++ function undefined-behavior
这是C++中的有效函数:
int f()
{
if(false)
{
return 42;
}
}
Run Code Online (Sandbox Code Playgroud)
以下定义导致UB:
int x = f(); //return value used
Run Code Online (Sandbox Code Playgroud)
问题: 以下表达式语句是否会导致UB?
f();
Run Code Online (Sandbox Code Playgroud)
从标准引用将非常受欢迎.谢谢
警告:关于文体学的评论是无关紧要的:)
C++03§6.6.3/ 2:
流出函数末尾相当于没有值的返回; 这会导致值返回函数中的未定义行为.
所以这是函数本身的UB.
BTW gcc给你一个很好的警告,指向这个UB:
In function 'int f()':
Line 7: warning: control reaches end of non-void function
Run Code Online (Sandbox Code Playgroud)