Topic: auto_ptr<T> suggestion
Author: Michael McKibben <mckibben@cerf.net>
Date: 1997/02/02 Raw View
I am new to this group so forgive me if someone has already suggested
this...
I would like to see the auto_ptr class in <memory> be changed so as to
support a user defined destructor. It would be an easy change. Just add
another template parameter that takes a function object that defines
operator()(T*). For example, if I defined the two following function
objects:
template<class _TYPE>
struct ptr_destructor {
void operator()(_TYPE * ptr) const
{
delete ptr;
}
};
template<class _TYPE>
struct array_destructor {
void operator()(_TYPE * ptr) const
{
delete[] ptr;
}
};
and auto_ptr was defined as
template <class _TYPE, class _D = ptr_destructor<_TYPE> >
class auto_ptr;
and all the delete ptr calls changed to _D()(ptr), then we could use
auto_ptr with new T[size] and user defined allocators/destructors. Has
this already been proposed? I don't have a copy of the latest WP.
-mike
------------------------------------
Michael McKibben <mckibben@cerf.net>
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jodle@bix.com (jodle)
Date: 1997/02/03 Raw View
Michael McKibben (mckibben@cerf.net) wrote:
: I would like to see the auto_ptr class in <memory> be changed so as to
: support a user defined destructor. It would be an easy change. Just add
: another template parameter that takes a function object that defines
: operator()(T*). For example, if I defined the two following function
: objects:
It might be simpler to have auto_ptr and auto_ptr_array as separate
generic classes. However, there is a critical flaw in using auto_ptr with
allocated arrays of objects. It doesn't provide a way for the consumer to
determine how many objects are in the array or prevent the consumer from
accessing objects that don't exist with indexes that are too large.
Therefore, providing an easy way to avoid using an appropriate class for
the job (for example, vector) that is prone to be misused may not be a
good feature to add to the standard library.
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]