Annex D (normative) Compatibility features [depr]

D.10 auto_ptr [depr.auto.ptr]

The class template auto_ptr is deprecated. [ Note: The class template unique_ptr ([unique.ptr]) provides a better solution.  — end note ]

D.10.1 Class template auto_ptr [auto.ptr]

The class template auto_ptr stores a pointer to an object obtained via new and deletes that object when it itself is destroyed (such as when leaving block scope [stmt.dcl]).

The class template auto_ptr_ref is for exposition only. An implementation is permitted to provide equivalent functionality without providing a template with this name. The template holds a reference to an auto_ptr. It is used by the auto_ptr conversions to allow auto_ptr objects to be passed to and returned from functions.

namespace std {
  template <class Y> struct auto_ptr_ref;   // exposition only

  template <class X> class auto_ptr {
  public:
    typedef X element_type;

    // [auto.ptr.cons] construct/copy/destroy:
    explicit auto_ptr(X* p =0) throw();
    auto_ptr(auto_ptr&) throw();
    template<class Y> auto_ptr(auto_ptr<Y>&) throw();
    auto_ptr& operator=(auto_ptr&) throw();
    template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
    auto_ptr& operator=(auto_ptr_ref<X> r) throw();
   ~auto_ptr() throw();

    // [auto.ptr.members] members:
    X& operator*() const throw();
    X* operator->() const throw();
    X* get() const throw();
    X* release() throw();
    void reset(X* p =0) throw();

    // [auto.ptr.conv] conversions:
    auto_ptr(auto_ptr_ref<X>) throw();
    template<class Y> operator auto_ptr_ref<Y>() throw();
    template<class Y> operator auto_ptr<Y>() throw();
  };

  template <> class auto_ptr<void>
  {
  public:
    typedef void element_type;
  };
}

The class template auto_ptr provides a semantics of strict ownership. An auto_ptr owns the object it holds a pointer to. Copying an auto_ptr copies the pointer and transfers ownership to the destination. If more than one auto_ptr owns the same object at the same time the behavior of the program is undefined. [ Note: The uses of auto_ptr include providing temporary exception-safety for dynamically allocated memory, passing ownership of dynamically allocated memory to a function, and returning dynamically allocated memory from a function. Instances of auto_ptr meet the requirements of MoveConstructible and MoveAssignable, but do not meet the requirements of CopyConstructible and CopyAssignable.  — end note ]

D.10.1.1 auto_ptr constructors [auto.ptr.cons]

explicit auto_ptr(X* p =0) throw();

Postconditions: *this holds the pointer p.

auto_ptr(auto_ptr& a) throw();

Effects: Calls a.release().

Postconditions: *this holds the pointer returned from a.release().

template<class Y> auto_ptr(auto_ptr<Y>& a) throw();

Requires: Y* can be implicitly converted to X*.

Effects: Calls a.release().

Postconditions: *this holds the pointer returned from a.release().

auto_ptr& operator=(auto_ptr& a) throw();

Requires: The expression delete get() is well formed.

Effects: reset(a.release()).

Returns: *this.

template<class Y> auto_ptr& operator=(auto_ptr<Y>& a) throw();

Requires: Y* can be implicitly converted to X*. The expression delete get() is well formed.

Effects: reset(a.release()).

Returns: *this.

~auto_ptr() throw();

Requires: The expression delete get() is well formed.

Effects: delete get().

D.10.1.2 auto_ptr members [auto.ptr.members]

X& operator*() const throw();

Requires: get() != 0

Returns: *get()

X* operator->() const throw();

Returns: get()

X* get() const throw();

Returns: The pointer *this holds.

X* release() throw();

Returns: get()

Postcondition: *this holds the null pointer.

void reset(X* p=0) throw();

Effects: If get() != p then delete get().

Postconditions: *this holds the pointer p.

D.10.1.3 auto_ptr conversions [auto.ptr.conv]

auto_ptr(auto_ptr_ref<X> r) throw();

Effects: Calls p.release() for the auto_ptr p that r holds.

Postconditions: *this holds the pointer returned from release().

template<class Y> operator auto_ptr_ref<Y>() throw();

Returns: An auto_ptr_ref<Y> that holds *this.

template<class Y> operator auto_ptr<Y>() throw();

Effects: Calls release().

Returns: An auto_ptr<Y> that holds the pointer returned from release().

auto_ptr& operator=(auto_ptr_ref<X> r) throw()

Effects: Calls reset(p.release()) for the auto_ptr p that r holds a reference to.

Returns: *this