Search code examples
c++codeblockscompiler-warningsunreachable-code

Why can I compile a code with 2 returns?


Since am comming from a java island, I wounder why the compiler doesnt warns about unreachable code in something like:

int main(int argc, char** argV)
{

    std::list<int> lst = {1,2,3,4};

    return 0;



    std::cout << "Done!!!" << std::endl;
    return 0;
}

my question:

Why can I compile a code with 2 returns?

my Compiler is gcc for c++11, on Windows, code block


Solution

  • I wounder why the compiler doesnt warns about unreachable code in something like

    It is pretty well explained in gcc documentaion about warnings:

    -Wunreachable-code

    Warn if the compiler detects that code will never be executed. This option is intended to warn when the compiler detects that at least a whole line of source code will never be executed, because some condition is never satisfied or because it is after a procedure that never returns.

    It is possible for this option to produce a warning even though there are circumstances under which part of the affected line can be executed, so care should be taken when removing apparently-unreachable code.

    For instance, when a function is inlined, a warning may mean that the line is unreachable in only one inlined copy of the function.

    This option is not made part of -Wall because in a debugging version of a program there is often substantial code which checks correct functioning of the program and is, hopefully, unreachable because the program does work. Another common use of unreachable code is to provide behavior which is selectable at compile-time.

    Though g++ 5.1.0 does not produce any warnings for this code even with this option enabled.