Is it possible to write statements after return?

后端 未结 5 1292
遇见更好的自我
遇见更好的自我 2021-01-20 18:13

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

#include 

        
相关标签:
5条回答
  • 2021-01-20 18:19

    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++?

    0 讨论(0)
  • 2021-01-20 18:20

    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.

    0 讨论(0)
  • 2021-01-20 18:22

    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.

    0 讨论(0)
  • 2021-01-20 18:29

    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
    }
    
    0 讨论(0)
  • 2021-01-20 18:36

    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.

    0 讨论(0)
提交回复
热议问题