This is a valid function in C++:
int f()
{
if(false)
{
return 42;
}
}
The following definition causes UB:
int x = f(); // return value used
The question: Does the following expression statement cause UB?
f();
Quote from the standard would be very much welcome.
C++03 §6.6.3/2:
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
So this is an UB in a function itself.
BTW gcc gives you a nice warning pointing to this UB:
In function 'int f()':
Line 7: warning: control reaches end of non-void function
C++03, §6.6.3/2: "Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function."
Note that the same is not true in C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With