What happens if you don't return a value in C++?

后端 未结 6 792
南方客
南方客 2020-12-28 18:56

Yesterday, I found myself writing code like this:

SomeStruct getSomeStruct()
{
    SomeStruct input;

    cin >> input.x;
    cin >> input.y;
}
<         


        
6条回答
  •  不思量自难忘°
    2020-12-28 19:29

    Falling off the end of a function that is declared to return a value (without explicitly returning a value) leads to undefined consequences. For gcc, you should start with the -Wall command line switch that turns on most useful warnings. The specific gcc warning that controls the warning you want is -Wreturn-type (which is included in -Wall, I just mention this for completeness).

    Once you have warnings turned on, you should also use -Werror to treat warnings as errors and make the build stop at the point it detects an error.

提交回复
热议问题