문제

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. Thanks

Warning: Remarks about stylistics are irrelevant :)

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top