Skip to main content

std::auto_ptr

Defined in header <memory>.

Deprecated in C++11, Removed in C++17
// 1)
template< class T > class auto_ptr;
// 2)
template<> class auto_ptr<void>;

auto_ptr is a smart pointer that manages an object obtained via new expression and deletes that object when auto_ptr itself is destroyed. It may be used to provide exception safety for dynamically allocated objects, for passing ownership of dynamically allocated objects into functions and for returning dynamically allocated objects from functions.

Copying an auto_ptr copies the pointer and transfers ownership to the destination: both copy construction and copy assignment of auto_ptr modify their right-hand arguments, and the "copy" is not equal to the original. Because of these unusual copy semantics, auto_ptr may not be placed in standard containers.

std::unique_ptr is preferred for this and other uses. (since C++11)

  1. Specialization for type void is provided, it declares the typedef element_type, but no member functions.

An additional class template auto_ptr_ref is referred to throughout the documentation. It is an implementation-defined type that holds a reference to auto_ptr. The implementation is allowed to provide the template with a different name or implement the functions returning it or accepting it as parameter in other ways.

Member types

pubelement_typeT

Member functions

pub(constructor)creates a new auto_ptr
(public member function)
pub(destructor)destroys an auto_ptr and the managed object
(public member function)
puboperator=transfers ownership from another auto_ptr
(public member function)
puboperator auto_ptr<Y>
operator auto_ptr_ref<Y>
converts the managed pointer to a pointer to different type
(public member function)

Observers

pubgetreturns a pointer to the managed object
(public member function)
puboperator*
operator->
accesses the managed object
(public member function)

Modifiers

pubresetreplaces the managed object
(public member function)
pubreleasereleases ownership of the managed object
(public member function)

std::auto_ptr

Defined in header <memory>.

Deprecated in C++11, Removed in C++17
// 1)
template< class T > class auto_ptr;
// 2)
template<> class auto_ptr<void>;

auto_ptr is a smart pointer that manages an object obtained via new expression and deletes that object when auto_ptr itself is destroyed. It may be used to provide exception safety for dynamically allocated objects, for passing ownership of dynamically allocated objects into functions and for returning dynamically allocated objects from functions.

Copying an auto_ptr copies the pointer and transfers ownership to the destination: both copy construction and copy assignment of auto_ptr modify their right-hand arguments, and the "copy" is not equal to the original. Because of these unusual copy semantics, auto_ptr may not be placed in standard containers.

std::unique_ptr is preferred for this and other uses. (since C++11)

  1. Specialization for type void is provided, it declares the typedef element_type, but no member functions.

An additional class template auto_ptr_ref is referred to throughout the documentation. It is an implementation-defined type that holds a reference to auto_ptr. The implementation is allowed to provide the template with a different name or implement the functions returning it or accepting it as parameter in other ways.

Member types

pubelement_typeT

Member functions

pub(constructor)creates a new auto_ptr
(public member function)
pub(destructor)destroys an auto_ptr and the managed object
(public member function)
puboperator=transfers ownership from another auto_ptr
(public member function)
puboperator auto_ptr<Y>
operator auto_ptr_ref<Y>
converts the managed pointer to a pointer to different type
(public member function)

Observers

pubgetreturns a pointer to the managed object
(public member function)
puboperator*
operator->
accesses the managed object
(public member function)

Modifiers

pubresetreplaces the managed object
(public member function)
pubreleasereleases ownership of the managed object
(public member function)