7

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.

4

2 に答える 2

9

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
于 2010-11-03T18:08:56.787 に答える
3

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.

于 2010-11-03T18:12:10.330 に答える