Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] shared_ptr template type
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2009-07-14 14:40:51

Hash: SHA1

On Friday 10 July 2009, Peter Dimov wrote:
> Frank Mori Hess wrote:
> > And also I imagine generalized_shared could be implemented by copying
> > the entire shared_ptr codebase and then making only minor changes.
> shared_count can already take an arbitrary P/D pair, so you only need to
> copy shared_ptr itself, not the rest of the codebase.

Ah, ok. Based on your encouragement (at least in the relative sense, since
everyone else's response has been negative :), I've implemented a first pass
at generic_shared. For those just tuning in generic_shared is like a
shared_ptr but can accept a more generic type as the "pointer". It can
accept either a plain old pointer as its template type, or a class which:

1) has operator->() and operator*()
2) either has value_type/reference/pointer member typedefs, or a
specialization of boost::smart_pointer_traits
3) has a is_null_pointer() free function findable by ADL
4) supports (in)equality comparison

generic_shared can also be used by another smart pointer class with similar
requirements, since it fulfills them all itself. So you could have
monitor<generic_shared<T*> > , to bring back the monitor example.

The code is in the file generic_shared.hpp (a fork of shared_ptr.hpp from
trunk) which is located in the sandbox directory:

The file can be copied into the main boost tree (I've been testing it with
trunk). There is also a shared_ptr.hpp in the same directory, which
implements shared_ptr on top of generic_shared. I used that mainly so I
could do some testing of generic_shared using the shared_ptr tests
unmodified. The tests pass, except for those which use
enabled_shared_from_this or weak_ptr. Beyond that, I've played around a
little with a few basic tests that use a user-defined pointer type, and also
use a list iterator as the pointer type (with a deleter that uses the
iterator to erase the element from its list).

So, the current status is: no generic_weak (analogous to weak_ptr) yet, and no
shared_from_this support. Also, there is an unresolved issue with respect to
casting for non-plain old pointer types. generic_shared tries to apply
boost::static/const/dynamic_pointer_cast to its pointer type, but ADL doesn't
work for boost::static/const/dynamic_pointer_cast, and specialization won't
work either. It seems I'll need to add a ADL hook for casts as suggested at
the end of this thread:

so user-defined pointer types can be support casting.

So, before I spend any more time on it, is this something worth pursuing
further as far as boost goes? Does it stand any chance of making it into the
smart_ptr library? Is there a silent majority out there who thinks this is a
worthwhile idea, or is it just me? I might even settle for a vocal
minority :)

Version: GnuPG v1.4.9 (GNU/Linux)


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