C++ auto_ptr for pointer to array

An auto_ptr can wrap a raw pointer but this raw pointer should not be a pointer to an array because auto_ptr will not release memory of the array when it goes out of scope. auto_ptr calls delete on the pointer when it goes out of scope. However for memory pointed to by an array pointer is to be freed then delete[] needs to be called and not delete.

How can you have an auto_ptr for a pointer to array?
Even before you try to answer that question, think whether you really need an auto_ptr for array pointer? Unless you are writing a low level library like STLHow, you most likely don’t need an auto_ptr for array pointer. That exactly is the reason why one is not included in the standard C++ library.

Instead use a Vector.

STLHow internally uses auto_ptr for pointer to array. Its implementation is listed below.
Check out STLHow – the STL that is meant to be readable.

			// an auto pointer to use with arrays
			// lets use arrays in create temp and swap idiom
			template 
			class auto_array
			{
			    public :
					auto_array(T *t) : ptr_( t )
					{ }

					~auto_array()
					{ delete[] ptr_; }

					T *operator->()
					{ return ptr_; }

					T *release()
					{ 
						T *tmp( ptr_ );
						ptr_ = 0;
						return tmp;
					}

					T &operator[](int i)
					{ return ptr_[i]; }

			    private :
					T *ptr_;
			};