Boost logo

Boost :

From: Daniel Frey (d.frey_at_[hidden])
Date: 2008-03-26 11:08:41

On Wed, 2008-03-26 at 09:26 -0400, Frank Mori Hess wrote:
> > > > does shared_from_this() also guarantee that it never throws?
> > >
> > > No, it can throw bad_weak_ptr.
> >
> > <> states that only
> > std::bad_alloc is thrown - if anything is thrown at all. Am I misreading
> > that?
> That's talking about the smart pointer classes. shared_from_this is a method
> of enable_shared_from_this, which isn't a smart pointer.

See last paragraph.

> > And reading the documentation for weak_ptr, it's not clear to me when a
> > bad_weak_ptr exception is thrown at all. All weak_ptr functions are
> > documented to never throw.
> It's thrown by the shared_ptr constructor that takes a weak_ptr argument.

But shared_from_this() requires that there must exist at least one
shared_ptr instance that owns the object, which means that the
use_count() is always guaranteed to be >0, hence the exception is never
thrown by shared_ptr's ctor.

Oh, and the ctor of shared_ptr therefore *can* throw something else than
std::bad_alloc, right? Unlike shared_from_this(), AFAICS.

Regards, Daniel

Boost list run by bdawes at, gregod at, cpdaniel at, john at