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 {
    unique_ptr( );
    unique_ptr( nullptr_t Nptr );
    explicit unique_ptr( pointer Ptr );
    unique_ptr( pointer Ptr,
        typename conditional<is_reference<Del>::value, 
            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( const unique_ptr& Right) = delete;
    unique_ptr& operator=(const unique_ptr& Right ) = delete;


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.

Use the make_unique helper function to efficiently create new instances of unique_ptr.

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft