Let's say that I'm making an application which will be long lasted, and not expected to terminate for any normal reasons (e.g. user terminates application, e.x.: an HTTP server).
I mark main itself with the C++11 standard attribute [[noreturn]], indicating that it must never return control back to the callee without aborting or throwing an exception.
[[noreturn]] int main(
int const argc,
char const * const[] argv
) { ... }
I start off by parsing the CLI arguments, and then pass what data was extracted into my "real" main function, where more of the work continues.
The other function, we'll call it realMain, is also marked as [[noreturn]].
In my code, I gave the main function a return statement into realMain, and promptly recieved a generic Clang warning about returning from a noreturn function.
[[noreturn]] int realMain(Data const data) {
...
std::cerr << "Unreachable!" << std::endl;
std::abort();
}
[[noreturn]] int main(...) {
...
auto const parsed_cli_args = parse(argv, argc);
...
return realMain(parsed_cli_args);
}
Is it safe to return into other noreturn functions, or is this undefined behavior?
(and unrelated, may any compilers take advantage of this? e.g. making main directly jmp instead of call realMain?)