"tragomaskhalos" <dave.du.vergier@logicacmg.com> a ?crit dans le message de
On 30 Jul, 19:50, "Xavier Serrand" <xxxxxx.xxxx...@xxx.fr> wrote:
template <typename T> static Pile<T> * createPile(int sz)
{
Pile<T> * pp;
try
{
pp = new Pile<T>(sz);
return pp;
}
catch (std::bad_alloc&)
{
try
{
// clearing what can be cleared ...
// ...
// deleting the instance : the destructor has to be sure
// (may be hard to do...)
pp->~Pile<T>();
}
catch (std::exception& e)
{
// nothing to do
}
return NULL;
}
}
This is wrong isn't it? If bad_alloc is thrown then
the object won't have been constructed, so you don't
want to be calling the destructor on 'pp' - UB and
all that (not to mention the fact that pp is
uninitialised in this case anyway). In any case,
using new(nothrow) saves you having to mess about
with bad_alloc in the first place.
But as Alf has pointed out it's all largely academic,
if your program reaches this point you're probably
scuppered anyway, which reinforces my earlier
suggestion of keeping it simple and just letting the
exception propagate out of the ctor.
object could hav been constructed and bad_alloc raised ... after if one or
....
....