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 {
public:
    unique_ptr( );
    unique_ptr( nullptr_t Nptr );
    explicit unique_ptr( pointer Ptr );
    unique_ptr( pointer Ptr,
        typename conditional<is_reference<Del>::value, 
            Del,
            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;
};

Parameters

  • Right
    A unique_ptr.

  • Nptr
    An rvalue of type std::nullptr_t.

  • Ptr
    A pointer.

  • Deleter
    A deleter function that is bound to a unique_ptr.

Exceptions

No exceptions are generated by unique_ptr.

Remarks

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 deleterdefault_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 deleterdefault_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.

Members

Constructors

unique_ptr::unique_ptr

There are seven constructors for unique_ptr.

Typedefs

deleter_type

A synonym for the template parameter Del.

element_type

A synonym for the template parameter Type.

pointer

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

Member Functions

unique_ptr::get

Returns stored_ptr.

unique_ptr::get_deleter

Returns a reference to stored_deleter.

unique_ptr::release

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

unique_ptr::reset

Releases the currently owned resource and accepts a new resource.

unique_ptr::swap

Exchanges resource and deleter with the provided unique_ptr.

Operators

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.

operator->

The member function returns stored_ptr.

operator*

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.

Requirements

Header: <memory>

Namespace: std

See Also

Reference

<memory>