Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

unique_ptr Class

Stores a pointer to an owned object. The object is owned by no other unique_ptr. The object is destroyed when the unique_ptr is destroyed.

template<class Type, class Del = default_delete<Type> >
    class unique_ptr {

        typedef Type element_type;
        typedef Del deleter_type;
        typedef T1 pointer;

        unique_ptr ();
        unique_ptr (
            nullptr_t _Nptr
        explicit unique_ptr (
            pointer _Ptr
        unique_ptr (
            pointer _Ptr,
            typename conditional<
                typename add_reference<const Del>::type
            >::type _Deleter
        unique_ptr (
            pointer _Ptr,
            typename remove_reference<Del>::type&& _Deleter
        unique_ptr (
            unique_ptr&& _Right
        template<class Type2, Class Del2>
            unique_ptr (
                unique_ptr<Type2, Del2>&& _Right

    ~unique_ptr ();

    unique_ptr& operator= (
        unique_ptr&& _Right
    template<class Type2, Class Del2>
        unique_ptr& operator= (
            unique_ptr<Type2, Del2>&& _Right
    void swap (
        unique_ptr& _Right
    pointer release ();
    void reset (
       pointer _Ptr = pointer()

    pointer get () const;
    Type& operator* () const;
    pointer operator-> () const;
    Del& get_deleter ();
    const Del& get_deleter () const;
    explicit operator bool () const;

        const unique_ptr& _Right
) = delete;
    unique_ptr& operator=(
        const unique_ptr& _Right
) = delete;

    pointer stored_ptr;    // exposition only
    Del stored_deleter;    // exposition only


A unique_ptr.


An rvalue of type std::nullptr_t.


A pointer.


A deleter function that is bound to a unique_ptr.

No exceptions are generated by unique_ptr.

The unique_ptr class supersedes auto_ptr, and can be used as an element of STL containers.

unique_ptr uniquely manages a resource. Each unique_ptr object stores a pointer to the object that it owns or stores a null pointer. A resource can be owned by no more than one unique_ptr object; when a unique_ptr object that owns a particular resource is destroyed, the resource is freed. A unique_ptr object may be moved, but not copied; for more information, see Rvalue Reference Declarator: &&.

The resource is freed by calling a stored deleter object of type Del that knows how resources are allocated for a particular unique_ptr. The default deleter default_delete<Type> assumes that the resource pointed to by _Ptr is allocated with new, and that it can be freed by calling delete _Ptr. (A partial specialization unique_ptr<Type[]>manages array objects allocated with new[], and has the default deleter default_delete<Type[]>, specialized to call delete[] _Ptr.)

The stored pointer to an owned resource, stored_ptr has type pointer. It is Del::pointer if defined, and Type * if not. The stored deleter object stored_deleter occupies no space in the object if the deleter is stateless. Note that Del can be a reference type.


There are seven constructors for unique_ptr.


A synonym for the template parameter Del.


A synonym for the template parameter Type.


A synonym for Del::pointer if defined, otherwise Type *.


Returns stored_ptr.


Returns a reference to stored_deleter.


stores pointer() in stored_ptr and returns its previous contents.


Releases the currently owned resource and accepts a new resource.


Exchanges resource and deleter with the provided unique_ptr.

operator bool

The operator returns a value of a type that is convertible to bool. The result of the conversion to bool is true when get() != pointer(), otherwise false.


The member function returns stored_ptr.


The member function returns*stored_ptr.

unique_ptr operator=

Assigns the value of a unique_ptr (or a pointer-type) to the current unique_ptr.

Header: <memory>

Namespace: std


Other Resources

© 2017 Microsoft