Is a return statement mandatory for C++ functions that do not return void?

前端 未结 7 1408
忘了有多久
忘了有多久 2020-11-27 07:31

My Herb Schildt book on C++ says: \"... In C++, if a function is declared as returning a value, it must return a value.\" However, if I write a function wit

相关标签:
7条回答
  • 2020-11-27 08:13

    It is mandatory--it is an undefined behavior when such function ends without returning anything (so compilers might actually implement some kind of special behaviour). However, there are some special cases.

    ::main is an exception, it is assumed that return 0; is at the end of its code.

    Also, you don't have to return a value in a function that does not return cleanly, f.e.:

    int Foo() {
        throw 42;
    }
    
    0 讨论(0)
提交回复
热议问题