|
Boost : |
From: Andrew Koenig (ark-mlist_at_[hidden])
Date: 2007-05-01 09:38:58
> Isn't it odd that this nonsense is legal c++?
Yes, sort of. Consider:
double foo(blah blah blah) {
blah blah blah
if (x >= 0)
return std::sqrt(x);
throw std::domain_error("x was negative in foo");
}
Here static analysis can tell you that control can never reach the }. But
what if we put the throw into a separately compiled function?
if (x >= 0)
return std::sqrt(x);
throw_domain_error("x was negative in foo");
Now the programmer may know, even though the compiler cannot, that control
still cannot reach the }. What would have standard do? Force the
programmer to insert a return statement that the programmer knows can never
be executed?
Worse: The C89 standard allows this:
double f() { /* no return */ }
int main() { f(); return 0; }
In other words, a C function that claims to return non-void is allowed to
fall off the end as long as its caller does not attempt to use the
function's value. Which means that if C++ required a function that returns
non-void to throw an exception when falling off the end, it would introduce
a C incompatibility.
So maybe I'm agreeing that it's odd--but at least there's a reason for it.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk