Boost logo

Boost :

From: Mark Borgerding (mborgerding_at_[hidden])
Date: 2000-04-10 21:06:27


Dave Abrahams wrote:

> > Allowing exceptions to be thrown
> >
> > ... from a smart pointer destructor is another arguable choice.
>
> I don't agree with this. Under the current language rules a destructor which
> throws an exception doesn't complete. It is unclear what is to be done with
> a partially-destroyed object. That almost makes it nonsense. This is totally
> apart from the fact that program termination is likely.
>

You are right. I thought the behavior was undefined, but the standard dictates (
15.5.1 ) that terminate should be called
" -- when the destruction of an object during stack unwinding exits using an
exception."

So by the time a smart pointer destructor might try to catch(...) the exception
during a stack unwind, it is already too late -- the program has exited.

BTW, I think MSVC allows this to slip by, but I'm not 100% sure.

>
> > This can be
> > problematic in situations where the destructor might be called because of
> > another exception being thrown. The behavior of std::auto_ptr is to allow any
> > exceptions out from the destructor.
>
> No, std::auto_ptr's destructor has an empty exception-specification, so no
> exceptions will propagate out of it, ever.
>

I guess I was remembering the CD2 behavior. I just looked again and realized that
the definition had changed in the final standard.

So if ~auto_ptr has an empty excep. spec. , why does boost::shared_ptr allow
exceptions out?
Shouldn't we change this to make it as consistent as possible with the auto_ptr
interface?

> > Allowing such exceptions can reduce the
> > safety of using smart pointers. On the other hand, allowing such exceptions
> > makes smart pointers behave more like automatic objects.
>
> What does this mean? Can't a smart pointer be an automatic object?

I didn't explain myself very well.

What I meant was that allowing excpetions out of the d'tor makes a smart pointer to
a Foo act like an automatic Foo object.
i.e. If Foo::~Foo can throw an exception , so can smartptr<Foo>::~smartptr<Foo>

Thanks for the comments. I will try to get an updated copy of the document in the
vault sometime tomorrow.

Mark


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