11 Jul
2002
11 Jul
'02
9:38 p.m.
--- In Boost-Users@y..., "Peter Dimov"
From: "Mark Storer"
So the only purpose of a weak_ptr is to be able to grab a shared_ptr from it (and to avoid circular references, of course)?
Yes. The "idiomatic" use is:
weak_ptr<X> wp;
// ...
if(shared_ptr<X> p = make_shared(wp)) { // use p } else { // target lost }
Well, you could write the weak_ptr<>::operator-> to "do the right thing": shared_ptr<T> weak_ptr<T>::operator->() { shared_ptr<T> p = make_shared(wp); if (!p) throw "something"; return p; } I can see several reasons to not want to do this (mostly to do with performance), though. Bill Kempf