RE: [Boost-Users] weak_ptr... no * or ->?
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)? --Mark Storer Software Engineer Cardiff Software #include <disclaimer> typedef std::disclaimer<Cardiff> Discard;
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 }
--- 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
On Thursday, July 11, 2002, at 02:38 PM, bill_kempf wrote:
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; }
As long as you don't use the result of -> with the & operator or pass it to a function taking a reference. I think the temporary lifetime is a little to subtle to be a good solution here. -- Darin
From: "bill_kempf"
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;
return shared_ptr<T>(*this); // same as above
}
Yes, I can, and I did - initially. Darin objected that such an operator-> would be confusing for weak_ptr users and people that read the code (too much hidden meaning in p->f(),) and it's better to be explicit.
--- In Boost-Users@y..., "Peter Dimov"
From: "bill_kempf"
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;
return shared_ptr<T>(*this); // same as above
}
Yes, I can, and I did - initially. Darin objected that such an operator-> would be confusing for weak_ptr users and people that read the code (too much hidden meaning in p->f(),) and it's better to be explicit.
Oh, I agree with the design decision. I just didn't feel comfortable leaving the particulars out of the reply to the original op. You can provide a working implementation, and for some people it might seem like the right thing to do, and if the op was in this camp (or someone else reading the thread) then the answer wasn't complete enough. I just was trying to help point out some of the obvious (to me) reasons it wasn't done that way for those who might not have seen it (as in it wasn't obvious to them). Bill Kempf
participants (4)
-
bill_kempf
-
Darin Adler
-
Mark Storer
-
Peter Dimov