alloc_ptr

An extension of std::auto_ptr. alloc_ptr will do everything that auto_ptr will do with the same syntax. Additionally alloc_ptr will deal with array new/delete:

  alloc_ptr<int, array_deleter<int>> a(new int[4]);
  // Ok, destructor will use delete[]
  
Remarks

By adding the array_deleter<T> template parameter you can enable alloc_ptr to correctly handle pointers to arrays of elements.

alloc_ptr will also work with allocators which adhere to the standard interface. This comes in very handy if you are writing a container that is templated on an allocator type. You can instantiate an alloc_ptr to work with an allocator with:

alloc_ptr<T, Allocator<T>, typename Allocator<T>::size_type> a;

The third parameter can be omitted if the allocator is always going to allocate and deallocate items one at a time (e.g. node based containers).

alloc_ptr takes full advantage of compressed_pair so that it is as efficient as std::auto_ptr. The sizeof(alloc_ptr<int>) is only one word. Additionally alloc_ptr will work with a reference to an allocator instead of an allocator (thanks to call_traits). This is extremely useful in the implementation of node based containers.

This is essentially the std::auto_ptr interface with a few twists to accommodate allocators and size parameters.