Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

unique_ptr Class

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

template< class T, class Del = default_delete<T> >
    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 T2, Class Del2> unique_ptr( unique_ptr<T2, Del2>&& Right );
    unique_ptr( const unique_ptr& Right) = delete;
    unique_ptr& operator=(const unique_ptr& Right ) = delete;
};
//Specialization for arrays:
template <class T, class D> class unique_ptr<T[], D> 
{   public:     
     typedef pointer;
     typedef T element_type;
     typedef D deleter_type;

     constexpr unique_ptr() noexcept;
     template <class U> explicit unique_ptr(U p) noexcept;
     template <class U> unique_ptr(U p, see below d) noexcept;
     template <class U> unique_ptr(U p, see below d) noexcept;
     unique_ptr(unique_ptr&& u) noexcept;
     constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
     template <class U, class E>
       unique_ptr(unique_ptr<U, E>&& u) noexcept;
  
     ~unique_ptr();
  
     unique_ptr& operator=(unique_ptr&& u) noexcept;
     template <class U, class E>
       unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
     unique_ptr& operator=(nullptr_t) noexcept;
  
     T& operator[](size_t i) const;
     pointer get() const noexcept;
     deleter_type& get_deleter() noexcept;
     const deleter_type& get_deleter() const noexcept;
     explicit operator bool() const noexcept;
  
     pointer release() noexcept;
     void reset(pointer p = pointer()) noexcept;
     void reset(nullptr_t = nullptr) noexcept;
     template <class U> void reset(U p) noexcept = delete;
     void swap(unique_ptr& u) noexcept;
  
    // disable copy from lvalue
     unique_ptr(const unique_ptr&) = delete;
     unique_ptr& operator=(const unique_ptr&) = delete;
   };
 }

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.

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<T> 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<T[]>manages array objects allocated with new[], and has the default deleter default_delete<T[]>, specialized to call delete[] _Ptr.)

The stored pointer to an owned resource, stored_ptr has type pointer. It is Del::pointer if defined, and T * 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.

unique_ptr::unique_ptr

There are seven constructors for unique_ptr.

deleter_type

A synonym for the template parameter Del.

element_type

A synonym for the template parameter T.

pointer

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

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.

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.

Header: <memory>

Namespace: std

Show:
© 2015 Microsoft