What should I do if I did not set the return value of a function whose prototype returns a value

I just spent 3 hours, maybe more, trying to find an error, segfault or bad_allow, depending on how I modified the code to understand which object was messing with glass or memory: I put all my objects in doubt ... I killed the classes ... (however, it turned out to be good, because in the end these classes turned out to be useless :-)) ...

But, really, the real mistake was simple: I did not write a return statement in a function that should return a value (in my case, I needed to return std::vector<boost::any> ).

I thought gcc (4.6.3) could not be compiled without it. And I feel even stranger remembering that something similar happened to me with ms visual 2010 .

So now I am wondering if it is legal to write a return statement? What happens in such cases?

Is this due to the fact that the main function, which can have a return value or not? Or should I consider switching to gcc 4.7 ?

+4
source share
2 answers

This behavior is undefined:

[C++11: 6.6.3/2]: [..] The flowing end of the function is equivalent to return with no value; this results in undefined behavior in the return function.

It compiles because:

  • the compiler is not required to diagnose it;
  • Diagnostics is not always trivial, so your compiler is not worried;
  • C ++ is a do-it-yourself language.
+9
source

If you compile the -Wall -Wextra -pedantic , it should give you a compiler warning about a function that should return a value, but does not.

+5
source

All Articles