"Daniel T. said:
You don't need an auto_ptr to transfer ownership, that is not one of the
reasons for its existence. auto_ptr's *sole* reason for existence is to
ensure that the delete isn't missed.
Then perhaps you can petition the standards committee to remove
auto_ptr's release() member function. You may submit your request
directly to me if you like. Or you can post it to comp.std.c++, and
I'll pick it up from there and create a formal defect report.
With a vector, the memory won't get leaked...
Please tell me how A<T>::foo(T* p) is going to assure ownership
possession of p while at the same time needing to acquire other
resources in order to do so. I'll be especially interested in how
vector plays a role if p is a pointer which was new'd with new[]. If
you're looking for a motivating example, consider a shared_ptr_array
constructor taking a T*.
Perhaps you'll come up with something using try/catch instead of a local
object? That's certainly a respectable approach, but not what I would
personally recommend (assuming the existence of an efficient smart
pointer that will delete with delete[]). It might look like:
void
A<T>::foo(T* p)
{
try
{
// do something here that might throw
// ...
a_member_ = p; // now A<T> owns the pointer
}
catch (...)
{
delete [] p;
throw;
}
}
Personally I'd take this alternative any day:
void
A<T>::foo(T* p)
{
smart_ptr<T[]> sp(p);
// do something here that might throw
// ...
a_member_ = sp.release(); // now A<T> owns the pointer
}
-Howard