简体   繁体   中英

Instructions after return statement in a C++ function

I've seen this C++ function in a ROS kobuki node ( kobuki-auto-docking ). I wonder if the while is dead code ? Or if it can be called for a misterious reason sometimes ?

void AutoDockingROS::spin()
{
    return;

    while(!shutdown_requested_){;}
}

Thanks for your helps,

In C++, nothing after return is executed.

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

  1. If the preprocessor #define s return to something else for a particularly odd build configuration then the code could run.

  2. Someone might be porting the code blindly to Java. In Java, code within a finally block does run after a return .

  3. It's possible the developer retained the line to test the syntactic validity of !shutdown_requested_

All unlikely scenarios (I have seen the first one in production by the way) but worth checking if you're going to undertake a large refactoring effort.

return语句后的所有内容永远不会被执行。

It is equivalent to this:

void AutoDockingROS::spin()
{
    return;

    // while(!shutdown_requested_){;}
}

Effectively, the programmer wanted to leave some code there, perhaps as a reminder. It is never executed.

返回后没有人可以调用代码......好吧也许只有查克诺里斯可以......

Nothing can be executed after the return statement until its within a condition. Put the code above the return statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM