Statements after return statement in C ++ - functions

I've seen this C ++ feature in ROS kobuki node ( kobuki-auto-docking ). I wonder if this is dead code? Or if it can be caused for different reasons sometimes?

void AutoDockingROS::spin()
{
    return;

    while(!shutdown_requested_){;}
}

      

Thanks for the help,

+3


source to share


5 answers


In C ++, nothing is executed after return

.

But you should always respect the code you see in front of you:



  • If the preprocessor is #define

    return

    for something else for a particularly complex build configuration, then the code might work.

  • Someone can blindly port the code to Java. In Java, the code inside the block is finally

    run after return

    .

  • The developer may have saved the string for syntactic validation !shutdown_requested_

All unlikely scenarios (by the way, I saw the first one in production), but it's worth checking to see if you will be putting in a lot of refactoring efforts.

+5


source


Everything after the instruction return

will never be executed.



+3


source


This is equivalent to this:

void AutoDockingROS::spin()
{
    return;

    // while(!shutdown_requested_){;}
}

      

In fact, the programmer wanted to leave some code there, perhaps as a reminder. It never gets executed.

+3


source


Nobody can call by code after returning ... Well, maybe only Chuck Norris can ...

+2


source


Nothing can be done after the statement return

before its state. Place the code above the operator return

.

0


source







All Articles