[prev in list] [next in list] [prev in thread] [next in thread] 

List:       boost-users
Subject:    Re: [Boost-users] boost::weak_ptr and boost::intrusive_ptr
From:       "Nat Goodspeed" <ngoodspeed () solidworks ! com>
Date:       2006-12-01 18:07:08
Message-ID: 94F7A8DD4408D8499C6812FE42E2D4B7349D4F () corp-mail4 ! solidworks ! swk
[Download RAW message or body]

> -----Original Message-----
> From: boost-users-bounces@lists.boost.org [mailto:boost-users-
> bounces@lists.boost.org] On Behalf Of Steven T. Hatton
> Sent: Friday, December 01, 2006 7:03 AM
> To: Boost-users@lists.boost.org
> Subject: [Boost-users] boost::weak_ptr and boost::intrusive_ptr
> 
> Intrusive pointers are, IMO, the correct mechanism to hold a
> tree together.  They are efficient, effective, and easy to
> understand.  They work better than shared pointers for
> recursive data structures.  Unfortunately the lack of a
> corresponding weak_ptr to go with boost::intrusive_ptr means I
> either use a raw pointer to the parent, devise a strategy for
> decrementing the pointers myself, or create my own weak
> pointer. 

[Nat] I've thought about this problem to some extent. This is the way I
see it so far.

The one big advantage of a weak_ptr over a raw pointer is that the
weak_ptr can tell you when its referent becomes invalid.

If your data structure requires storing a pointer in a place which
itself becomes invalid as soon as the referent becomes invalid (e.g.
recursive deletion of a subtree), then I'm not convinced that weak_ptr
is significantly better than a raw pointer.

(Even "snipping loose" a subtree, but preserving it, doesn't seem to
argue for weak_ptr as a parent pointer. In such a case, you'd want to
clear the parent pointer anyway, wouldn't you?)

Does this rationale make sense, or am I overlooking something?

I'm not rejecting the idea of a weak_ptr that works with intrusive_ptr.
I'm just saying that for certain use cases, raw pointers seem to address
the requirements equally well -- with less runtime overhead. This case
looks like one of them.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic