[c++0x] shared_ptr get() and use_count() inconsistency
Rodolfo Lima
rodolfo@rodsoft.org
Thu Jun 5 18:15:00 GMT 2008
Peter Dimov escreveu:
>> The current standard draft says that use_count() should return 0 if
>> the shared_ptr is empty, and get() should return NULL if the
>> shared_ptr is also empty.
>
> http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#711
Thanks Peter. But I prefer the second solution you gave because it would
lead to, IMHO, a sane smart pointer semantics.
Say you want to pass a weak_ptr to a function but only if it points to a
not empty shared_ptr. One might write:
void func(std::weak_ptr<int> x) {}
std::shared_ptr<int> aux();
int i;
std::shared_ptr<int> sptr(aux, &i);
if(sptr)
func(sptr);
In this case func would receive an weak_ptr that doesn't point to
anything, although it was coded to not allow this, I mean, one would
expect that if ptr.use_count()==0, (bool)ptr == false, but this doesn't
hold above.
I know this is offtopic to this list, so I'm moving this to
comp.lang.c++.moderated
More information about the Libstdc++
mailing list