|
From: Alberto Ganesh Barbati on 16 Jun 2008 07:56 Greg Herlihy ha scritto: >> It boils down to what really is a smart pointer. >> One fundamental concept is that it manage the pointer life time, be it >> directly or indirectly through another smart pointer (the aliasing >> case). For me the empty/non-null case just doesn't fit in this >> concept, as being empty implies it doesn't manage anything, so there's >> no point in having a stored pointer (which would be unmanaged, which >> is the contrary of the original fundamental concept). > > Yes, the stored pointer of an "empty" shared_ptr is not managed by the > empty shared_ptr - but is instead managed (indirectly) by the > shared_ptr it aliases. That is wrong. If you alias a non-empty shared_ptr, you get a non-empty shared_ptr. Both will manage the same "owned" pointer while having two different "stored" pointers. In fact, the only effective way to have a empty shared_ptr with a non-null stored pointer is to alias shared_ptr() (which is empty by definition). > Furthermore, under either proposed fix for #711, the assert() in your > original program can fail (when the aliased shared_ptr is not empty). > For example: > > // aliasing shared_ptr > shared_ptr<int> aux(new int); > int i; > shared_ptr<int> sptr(aux, &i); > weak_ptr<int> wptr; > > if (sptr) > { > wptr = sptr; > assert(wptr.lock() == sptr); // assertion failure! > } > > Essentially, attempting to monitor an empty, aliased shared_ptr with a > weak_ptr will always fail - no matter how Issue #711 is resolved. Your reasoning is incorrect. sptr is *not* an empty shared_ptr in this case and the assertion will *not* fail. HTH, Ganesh -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: Are so many subclasses such a good idea? Next: Safely redirecting std::cout? |