|
Boost Users : |
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2006-12-09 11:17:44
David Grüner wrote:
> Hello,
> i need to give up the ownership of a ptr_container (vector here)
> over its pointers (without assignment to a new ptr_container).
> Two questions here:
>
> 1. Do i really have to write aPtrContainer.release().release()?
> It looks weird. For me, the function name release() of the container
> seems confusing since an unassigned aPtrContainer.release() just
> does the "opposite" of auto_ptr<T>::release(), namely deleting the
> pointers. ;o)
Right, there is a difference between a single smart pointer and a
container here.
Even if release() in a pointer container returned ptr_container*,
it would still not be consistent with auto_ptr<T>, since that class
returns T*, but ptr_container<T>, would return ptr_container<T>*.
But one could say, why not simply let release() return
container<T*>? That function cannot be code more efficiently as a member
function (due to the over encapsulated interface of eg. std::vector).
You have many options here, say
template< class PtrSequence >
std::vector< typename PtrSequence::value_type >
release( PtrSequence& seq )
{
std::vector< typename PtrSequence::value_type > result;
while( !seq.empty() )
result.push_back( seq.pop_back().release() );
std::reverse( result.begin(), result.end() );
}
> The behavior is more like transfer_ownership()
> or something similar ($0.02).
>
> 2. Beside managing memory, i plan to use ptr_container primarily to
> propagate const. So, in some cases i need a ptr_container which just
> doesn't own any given memory. What's the best way to do that?
use a non-owning clonse_allocator:
http://www.boost.org/libs/ptr_container/doc/reference.html#class-view-clone-allocator
-Thorsten
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