Boost logo

Boost :

From: Guillaume Melquiond (gmelquio_at_[hidden])
Date: 2003-05-20 06:23:47


On Tue, 20 May 2003, Giovanni Bajo wrote:

> 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" ?

You completely missed my point. And I never even thought about something
like "We want you to put a return non-zero after a throw." What I was
trying to say is that kind of code is ill-formed according to the Standard
if rand returns 0 during an execution:

void* operator new(std::size_t) {
  if (rand()) { /* do something useful */ }
  else return 0; // the program is ill-formed if this line is reached
}

And so I'm not surprised that a compiler complains if it sees a "return
0;" in an allocator without an empty exception-clause. The compiler could
probably do better and try to find if the "return 0;" statement may or may
not be reached. For example, due to the structure of the program, maybe
the rand() function will never return 0 in the previous allocator and
consequently no null pointer would ever be returned by the allocator and
so the allocator would be well-formed. But the user is playing with fire
and it's not a bad thing the compiler warns about the presence of "return
0;".

That was my point: as soon as the user put a "return 0;" in an allocator
without an exception-clause, there is the possibility of the user
forgetting or misunderstanding something.

Regards,

Guillaume


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk