Re: Sequence points in new operator and constructor
On 15 Des, 03:27, alan_mckenn...@yahoo.com wrote:
On Dec 10, 10:33 am, Chaos.A....@gmail.com wrote:
So, this code will be exception safe if there is a sequence point
between memory allocation (new operator), which may throw
std::bad_alloc, and constructor.
Is there any guarantee that new never throws it's exception AFTER
left
side with all arguments starts calculating?
It's been 4 days, and no one has answered the
original question:
Is "operator new()" required to complete (or at least
get beyond the point where it could throw) before any
constructor arguments are evaluated?
That is, is "Foo *foo = new Foo( f() )"
more like: "void *mem = operator new(size); Foo *foo = Foo( mem,
f() ); "
or like "Foo *foo = Foo( operator new(size), f() );"
5.3.4/21:
"Whether the allocation function is called before evaluating the
constructor arguments or after evaluating the constructor arguments
but before entering the constructor is unspecified. It is also
unspecified whether the arguments to a constructor is evaluated if the
allocation function returns the null pointer or exits using an
exception."
So a conforming implementation could do this:
1. evaluate ctor arguments
2. call allocation function // could throw.
4. enter the ctor
So the original example can't be exception safe. Neither is the
following analogous example:
int * f() { return new int(5); }
// in some function
auto_ptr<int> * p = new auto_ptr<int>( f() );
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]