共用方式為


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:
        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<
                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 ();

    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;

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

private:
    pointer stored_ptr;    // exposition only
    Del stored_deleter;    // exposition only
    };

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.

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.

Requirements

Header: <memory>

Namespace: std

See Also

Reference

<memory>

Other Resources

unique_ptr Members

<memory> Members