Boost logo

Boost :

Subject: [boost] [ptr_container] ptr_vector transfer from C array of pointers documentation
From: Number Cruncher (number.cruncher_at_[hidden])
Date: 2010-07-28 08:35:21

  The documentation for ptr_vector::transfer( iterator before, T**
from, size_type size, bool delete_from = true ) implies that the
operation "delete [] from" is executed, but having examined the source,
I don't think it is. This is actually the behaviour I need.

I'm trying to convert a std::vector<T*> to a boost::ptr_vector<T*>, i.e.
I have a vector of pointers which is solely responsible for "owning" the
objects being pointed to, and I want to create a ptr_vector which will
automatically clean up the objects when it goes out of scope. I need to
do the transfer so that no memory leaks in the event of an exception.

As far as I can see, this is exactly what ptr_vector::scoped_deleter is
doing: it will delete any of the copied pointers, but it never actually
deletes the "from" pointer; presumably because you don't know whether
operator new[] was used when allocating it.

Can anyone clear up/confirm that this will work:
   std::vector<T*> v1;
   v1.push_back(new T);

   boost::ptr_vector<T> v2;
   if (!v1.empty()) {
     v2.transfer(v2.end(), &v1[0], v1.size(), true);
   // from this point, don't care about v1 and v2 will handle deletion
of T objs


Boost list run by bdawes at, gregod at, cpdaniel at, john at