Is it possible to write statements after return?

与世无争的帅哥 提交于 2021-01-20 07:25:45

问题


Is the return statement the last statement inside main or is it possible to write statements after return?

#include <iostream>

using namespace std;

int main() {

    cout << "Hello" << endl;

    return 0;

    cout << "Bye" << endl;
}

This program compiles but only displays "Hello".


回答1:


is it possible to write statements after return?

It is possible and valid to write more statements after the return. With gcc and Clang, I don't get a warning, even with the -Wall switch. But Visual Studio does produce warning C4702: unreachable code for this program.


The return statement terminates the current function, be it main or another function.

Even though it is valid to write, if the code after the return is unreachable the compiler may eliminate it from the program as per the as-if rule.


You could have the return statement executed conditionally and you could have multiple return statements. For example:

int main() {
    bool all_printed{false};
    cout << "Hello" << endl;
    if (all_printed) 
        return 0;
    cout << "Bye" << endl;
    all_printed = true;
    if (all_printed) 
        return 0;
}

Or, you could use a goto before and after the return and some labels, to execute the return statement after the second output:

int main() {

    cout << "Hello" << endl;
    goto print;

return_here:
    return 0;

print:
    cout << "Bye" << endl;
    goto return_here;
}

Prints:

Hello
Bye

Another solution, linked to in this answer, would be to use RAII to print after the return:

struct Bye {
    ~Bye(){ cout << "Bye" << endl; } // destructor will print
};

int main() {
    Bye bye;
    cout << "Hello" << endl;
    return 0; // ~Bye() is called
}



回答2:


is return statement the last statement inside main or is it possible to write statements after return?

The run time behavior of executing a return statement is independent of the function. Be it main or some other other function, when a return statement is executed, nothing after that is executed in the function.

It is possible to write statements after the return statement. They are superfluous since they are not executed. A smart compiler might be able to even omit creation of object code corresponding to the statements after the return statement.




回答3:


You can write statements after a return like in your example, but they will never be executed, and some compilers will give you a warning "unreachable code" (For example C4702 in VS2015).

To execute code after a return statement, see Is it possible to execute code after return statement in C++?




回答4:


Such code is usually generated in the intermediates debugging stages while narrowing the window to capture a bug. It works but must be fixed by the programmer asap.




回答5:


The only way you could usefully have code after a "return" is if the "return" statement is conditional.

if (error) return 0;
# All code invoked past this point knows error is false

This isn't great coding style, you're creating code where there are 2+ ways to exit. When you need to debug it you'll find it more challenging to put in flags and "what happened when we exited" and so forth.



来源:https://stackoverflow.com/questions/50027068/is-it-possible-to-write-statements-after-return

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!