Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-06-22 09:25:10


Thorsten Ottosen wrote:
> Seweryn Habdank-Wojewódzki skrev:
>> Hi!
>>
>> Is there any smart_ptr and ptr_container that can transfer ownership
>> of pointer, to avoid useless copy-construction and destruction of
>> the object.
>
> I guess you can't release a shared_ptr, even though it's use count is
> 1.
> Peter, any reason for not having a release() given that its
> precondition is unique() is true?

The precondition is subject to race conditions with weak_ptr::lock. Even if
you get back a T* from shared_ptr<T>::release, you won't necessarily be able
to destroy it properly since you have no idea where it points. It could be a
pointer to a static object, a pointer to a base class without a virtual
destructor, a pointer to a member of type T. If you want release(),
shared_ptr is not the right tool. auto_ptr or unique_ptr offer release().

The problem today (in a pre-rvalue ref world) is that (a) we have no
unique_ptr and (b) even if we had, vector< unique_ptr > still wouldn't work.
I'd have expected ptr_vector<T> to be essentially an alias for vector<
unique_ptr<T> > since there's obviously a need for that, but apparently it
doesn't quite work that way.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net