|
Boost : |
From: Giovanni Bajo (giovannibajo_at_[hidden])
Date: 2003-05-20 05:21:06
Guillaume Melquiond <gmelquio_at_[hidden]> wrote:
> Next, the Standard says "If an allocation function declared with an empty
> exception-specification [snip]. Any other allocation function [so they are
> speaking about functions that do not have an empty exception-
> specification] that fails to allocate storage shall only indicate failure
> by throwing an exception of class [snip]".
Exactly. After throwing an exception, you are not supposed to return
anything, because the function will never return a value. So it does not
really matter what you write in your code! Do you really believe that the
standard is prescribing how to code a part of your code which will obviously
never execute? Do you really believe that it's saying "We want you to put a
return non-zero after a throw. We know it will never be executed, that code
for it will be never generate because optimizers will strip it out, but
nonetheless we are the standard and we require it to exist and to be a
non-zero value" ?
> Consequently, functions that do not have an empty exception-specification
> cannot return a null pointer when they are successful (since a null
> pointer is not allowed in case of success)
Yes, of course null pointer is not allowed in case of success (the user will
dereference it).
> and they cannot return a null
> pointer whey they fail (since a failure need to be indicated "by throwing
> an exception" for "any other allocation function")
The standard is saying that, if the function fails, it needs to throw an
exception. They say nothing about the return value in that situation,
because A FUNCTION THAT THROWS WILL NEVER RETURN A VALUE.
> Yes, I completely agree. It's the reason why I would have simply removed
> the return statement if it was only my decision. But since the maintainer
> of the file already had decided to put this statement to suppress some
> warnings, I wanted to modify it so that this already faulty code does
> annoy one less compiler (GCC) than it is already annoying (Comeau and
> Borland).
I will shortly submit a bug to GCC about your code snippet. I think their
language lawyers will agree that it's wrong in that situation.
This said, I obviously don't care if the function returns 0, 1 or 12345. If
it helps shut warning down, it's fine. But it's not because of standard
compliance: it's just because otherwise you can't compile the code with GCC.
Giovanni Bajo
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk