Auto ptr

Deprecated class of smart pointers in C++


title: "Auto ptr" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["articles-with-example-c++-code", "articles-with-underscores-in-the-title", "c++-standard-library"] description: "Deprecated class of smart pointers in C++" topic_path: "general/articles-with-example-c-code" source: "https://en.wikipedia.org/wiki/Auto_ptr" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Deprecated class of smart pointers in C++ ::

In the C++ programming language, **** (for automatic pointer) is an obsolete smart pointer class template that was available in previous versions of the C++ standard library (declared in the `` header file), which provides some basic RAII features for C++ raw pointers. It has been replaced by the unique_ptr class.

The auto_ptr template class describes an object that stores a pointer to a single allocated object that ensures that the object to which it points gets destroyed automatically when control leaves a scope.

The characteristics of auto_ptr are now considered unsatisfactory: it was introduced before C++11's move semantics, so it uses copying for what should be done with moves (and confusingly sets the copied-from auto_ptr to a null pointer). These copy semantics mean that it cannot be used in STL containers.

The C++11 standard made auto_ptr deprecated, replacing it with the [unique_ptr](smart-pointer-unique-ptr) class template.{{cite web |title=Working Draft, Standard for Programming Language C++ N3242 |url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf |accessdate=2013-02-17|page=1233|date=28 February 2011}}{{cite web|last=Kalev |first=Danny|title=Using unique_ptr, Part I|url=http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400|publisher=informIT|accessdate=30 September 2010}} auto_ptr was fully removed in C++17.{{cite web |title=Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190 |url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm |accessdate=2016-08-29|date=2014-10-09}} For shared ownership, the [shared_ptr](shared-ptr) template class can be used. shared_ptr was defined in C++11 and is also available in the Boost library for use with previous C++ versions.

Declaration

The auto_ptr class is declared in ISO/IEC 14882, section 20.4.5 as: ::code[lang=cpp] namespace std { template struct auto_ptr_ref {};

template <class X>
class auto_ptr {
public:
    using element_type = X;

    // 20.4.5.1 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>) throw();

    ~auto_ptr() throw();

    // 20.4.5.2 members:
    X& operator*() const throw();
    X* operator->() const throw();
    X* get() const throw();
    X* release() throw();
    void reset(X* p = 0) throw();

    // 20.4.5.3 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();
};

} ::

Semantics

The auto_ptr has semantics of strict ownership, meaning that the auto_ptr instance is the sole entity responsible for the object's lifetime. If an auto_ptr is copied, the source loses the reference. For example: ::code[lang=cpp] #include #include

using std::auto_ptr;

int main(int argc, char* argv[]) { int* a = new int[10]; auto_ptr<int[]> x(a); auto_ptr<int[]> y;

y = x;

std::cout << x.get() << std::endl; // Print NULL
std::cout << y.get() << std::endl; // Print non-NULL address of a

return 0;

} ::

This code will print a NULL address for the first auto_ptr object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=). The raw pointer i in the example should not be deleted, as it will be deleted by the auto_ptr that owns the reference. In fact, new int could be passed directly into x, eliminating the need for i.

Notice that the object pointed by an auto_ptr is destroyed using operator delete; this means that you should only use auto_ptr for pointers obtained with operator new. This excludes pointers returned by [malloc/calloc/realloc](malloc), and pointers to arrays (because arrays are allocated by operator [new[](new-c)] and must be deallocated by operator delete[]).

Because of its copy semantics, auto_ptr may not be used in STL containers that may perform element copies in their operations.

References

References

  1. "auto_ptr Class". Microsoft.
  2. (2014). "Effective Modern C++: 42 specific ways to improve your use of C++11 and C++14".
  3. (2004-07-01). "Collecting Shared Objects". Dr. Dobb's.

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

articles-with-example-c++-codearticles-with-underscores-in-the-titlec++-standard-library