Boost logo

Boost :

From: Gary Powell (Gary.Powell_at_[hidden])
Date: 2001-04-12 15:46:25


With Howard Hinnant's help (core code) I worked up an alternative
auto_array, specifically for those compilers that are actually C++
compliant, and this does not include MSVC 6.5 and gcc 2.95.2. This is an
alternative implementation to auto_array_ptr.hpp.

namespace boost {

template<class T, class traits = PickTraits<T>::traits >
class auto_ptr;

Which means:

boost::auto_ptr<X> a; // same as std::auto_ptr

boost::auto_ptr<X[]> ar; // same as other boost::auto_ptr<X[]>

boost::auto_ptr<X, mytrait> am; // user's can specialize the traits.

Current traits have two static members, and one typedef The most important
is a "dispose" function.

template<class T>
struct single {
static void dispose(T *p) { delete p; }
}

template<class T>
struct array {
static void dispose(T *) { delete [] p; }

There is also a category tag so that you can't call operator[] on a plain
auto_ptr<X>, but you can on a auto_ptr<X[]>.

(tested with a static compile time assert.)
(I welcome other suggestions on how to do this correctly.)

There is also a "rebind()" call which attempts to prevent slicing.

So please take a look. And let me/Greg and Howard know what sort of
interface you want.

Also someone with more patience than I have could look at MSVC and gcc and
tell me why they don't delete the memory correctly. (Metrowerks seems to do
the right thing for both sets of code.) But I am also curious as to whether
any other compiliers do the right thing or is this a fluke of their memory
layout.

I like this traits method but unless I can get MSVC working, I can't use it
in my day job.

  Yours,
 -gary-

gary.powell_at_[hidden]

PS
  The two new files are in the vault named auto_ptr.hpp and
auto_ptr_test.cpp


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk