Example:
int foo() { throw 0; }
int bar() { foo(); }
foo
here doesn't get a warning, while bar
gets the error "must return a value". More typically, bar
would have some return statements and get the warning "not all control paths return a value".
Why isn't an indirect throw
recognized as a proper way to exit the function? How can I work around it, ideally by modifying foo
and not bar
?
The actual example I have in mind is that I have a ThreadSafeExit
function which calls the test platform's Exit
function, and I hoped that adding throw 0
at the end would get rid of warnings.
You should be able to quiet the compiler by applying the noreturn attribute to foo.
[[noreturn]]int foo() { throw 0; }
int bar() { foo(); }
However in this case, I would really fix it this way:
int foo() { throw 0; }
int bar() { return foo(); }
This will also make the compiler happy and is a lot more natural to read, since it is very common to invoke another function to calculate your return value. The first way can leave someone scratching their head as to what is going on if foo and bar are not close to each other in code.
You can do both, of course to be super clear about the intent.