Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-03-08 10:57:54


Joe Gottman wrote:
> I just realized that it would be impossible to use weak_ptr with
> any
> hash. The problem is that the hash function would have to use
> weak_ptr's get() method. Then if the underlying object of the
> weak_ptr gets destroyed, get() will suddenly return 0 instead of the
> previous pointer, so the hash function will now map the weak_ptr to a
> new bucket. Is there any way to
> add a member function that returns a const void * that will be
> invariant
> even if the underlying object of the weak_ptr has been destroyed?

weak_ptr no longer has get().

You are right that currently shared_ptr and weak_ptr work as std::map keys
but not as hash_map keys, and that we need to add _some kind_ of hash table
support. One easy approach is to supply a "size_t hash_value() const" member
function; this will allow "expert" users to use shared_ptr/weak_ptr in a
hash table by creating an appropriate hash function object _and_ equivalence
object (important since shared_ptr/weak_ptr equivalence isn't a == b but !(a
< b) && !(b < a)).

I would rather prefer to make hash_set and hash_map work "out of the box"
with a shared_ptr and weak_ptr. Unfortunately, there is no standard hash_*
interface yet, and the formal proposal by Matt Austern doesn't address the
problem well.


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