1

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

6 Answers6

5

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.

Community
  • 1
  • 1
Slava
  • 43,454
  • 1
  • 47
  • 90
1

Why shouldn't you be able to compile code that has multiple returns? Because the code is unreachable? Most compilers can issue a warning for that.

However, I often see code like:

   if(a)
   {
      // Do stuff
   }
   else
   {
      // Do other stuff

      if(b)
      {
          // Do more stuff
      }
      else
      {
          // Do other more stuff
      }
   }

That could be simplified as

   if(a)
   {
      // Do stuff
      return;
   }

   // Do other stuff

   if(b)
   {
      // Do more stuff
      return;
   }

   // Do other more stuff

About a decade ago, people frowned on having more than one return in a function of method, but there really is no reason to continue frowning on it with modern compilers.

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
0

Because this part

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

will never be called because of the first return statement, but it is not an error aborting the compilation, rather the compiler might drop a warning, depending on what compiler you are using (e.g. Microsofts VC++ compiler warns you about that).

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
0

Mainly because more often than not the compiler cannot know for sure. (There have been attempts to do this in Java but there the criteria for defining reachability have been decided upon.)

In this case, indeed, it is obvious.

Some compilers do issue reachability warnings but the C++ standard does not require this.

No answer on reachability is complete without referencing this: https://en.wikipedia.org/wiki/Halting_problem

As a final remark on Java, consider these two Java snippets:

if (true){
    return;
}
; // this statement is defined to be reachable

and

while (true){
    return;
}
; // this statement is defined to be unreachable

The worst of both worlds is attained, in my humble opinion.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Related: Java warns *too often*, because like said 100% correctness is impossible. Not just for reachability things, but also for things like code paths without initialization etc. – deviantfan Apr 12 '17 at 14:22
0

Unreachable code is not a compile error in C++, but usually gives a warning, depending on your compiler and flags.

You can try to add -Wall option when you call your compiler. This will active many useful warning.

baddger964
  • 1,199
  • 9
  • 18
0

There are two reasons for this:

  1. C++ has many standards (c++11, c++14, c++17 etc. ) unlike java (java is very rigid in standard and only thing that matters really with java is the version you are using), so, some compilers might warn you about the unreachable code while others might not.

  2. The statements after return 0, though are unreachable logically, do not cause any fatal error like ambiguity, syntax error, etc. and can be compiled easily (if the compiler wills to ;) ).

officialaimm
  • 378
  • 4
  • 16
  • `C++ has many standards (c++11, c++14, c++17 etc. ) unlike java` Not unlike Java. The language Java has versions too, and that's not equal to the software version. – deviantfan Apr 26 '17 at 13:26