Class Template unique_ptr

Description
Examples
Headers
Reference
Synopsis
Class Template unique_ptr

Description

The class template unique_ptr is a Smart Interface Pointer with the following characteristics:

Ownership may be transfered from an lvalue of type unique_ptr using the function template move.

unique_ptr is patterned on the template move_ptr introduced in [Hinnant1] as a safe replacement for std::auto_ptr. A free implementation of move_ptr is available here: Boost.MovePtr.

Examples

I. Receiving Ownership

By declaring a function to return a specialization of unique_ptr, the programmer indicates that the function transfers ownership of the managed object to the caller and ensures that the object is freed if the caller ignores the return value.

    unique_ptr<IScrollbar> NewScrollbar(const Style& style);

II. Relinquishing Ownership

By declaring a function with a parameter whose type is specialization of unique_ptr, the programmer indicates that the function takes ownership of the managed object and ensures that the object will be freed unless it is explicitly released.

    void ListView::AddItem(unique_ptr<ListItem> item);

III. RAII

unique_ptr can be used like std::auto_ptr to ensure that resources are cleaned up at the end of a block in an exception-safe manner.

    void f()
    {
        unique_ptr<IAnimal> animal(new Hound);
        std::cout << "name = " << animal->name() << "\n";
        animal->eat();
        animal->speak();
    }

In the above example, the dynamically allocate Hound is deleted before the function f finishes execution, even if an exception is thrown.

Headers

<boost/interfaces/unique_ptr.hpp>

Reference

Synopsis

namespace boost { namespace interfaces {

template<typename Interface>
class unique_ptr {
public:
    typedef typename fixed_view<Interface> element_type;

        // Constructors

    unique_ptr();

    template<typename T>
    explicit unique_ptr(T* t);

    unique_ptr(const unique_ptr& ptr);

    template<typename Subinterface>
    unique_ptr(const unique_ptr<Subinterface>& ptr);

    template<typename Subinterface>
    unique_ptr(move_source< unique_ptr<Subinterface> > src);

        // Destructor

    ~unique_ptr();

        // Assignment

    unique_ptr& operator=(unique_ptr ptr);

    template<typename Subinterface>    
    unique_ptr& operator=(unique_ptr<Subinterface> ptr);

        // Smart pointer interface

    element_type* get() const;

    element_type& operator*() const;

    element_type* operator->() const;

    manual_ptr<Interface> release();

    void reset();

    template<typename T>
    void reset(T* t);

    operator unspecified-bool-type() const;

    void swap(unique_ptr& p);
};

} } // End namespace boost::interfaces

Class Template unique_ptr

Template Parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

unique_ptr::element_type

    typedef typename fixed_view<T> element_type;

Used by the dereferencing operators, in place of the template parameter Interface, to help prevent resource leaks and double deletes. See Example.

unique_ptr::unique_ptr

    unique_ptr();

Constructs an empty instance of unique_ptr. Never throws.

    template<typename T>
    explicit unique_ptr(T* t);
Template Parameters
T-

A class implementing Interface. The type T must be complete and have an accessible destructor, but need not be complete at the point t is freed.

Constructs an instance of unique_ptr which takes ownership of the given object. Unless, after zero or more transfers of ownership, the object is released using unique_ptr::release, it will eventually be freed correctly using delete.

unique_ptr::unique_ptr
    unique_ptr(const unique_ptr& ptr);

    template<typename Subinterface>
    unique_ptr(const unique_ptr<Subinterface>& ptr);

Constructs a copy of the given instance of unique_ptr, taking ownership of the managed object. Never throws.

    template<typename Subinterface>
    unique_ptr(move_source< unique_ptr<Subinterface> > src);

Constructs an instance of unique_ptr from the return value of the function move, taking ownership of the object managed by the instance of unique_ptr on which move was invoked. Allows explicit transfer of ownership from an lvalue of type unique_ptr when implicit tranfer would be forbidden.

Never throws.

unique_ptr::~unique_ptr

    ~unique_ptr();

If there is currently a managed object, frees it using delete.

Throws only if the destructor of the bound object throws.

unique_ptr::operator=

    unique_ptr& operator=(unique_ptr ptr);

    template<typename Subinterface>    
    unique_ptr& operator=(unique_ptr<Subinterface> ptr);

Takes ownership of the pointer managed by the given instance of unique_ptr. Never throws.

unique_ptr::get

    element_type* get() const;

Returns a pointer through which member functions of the bound object can be accessed. Never throws.

unique_ptr::operator*

    element_type& operator*() const;

Returns a reference through which member functions of the bound object can be accessed. Never throws.

unique_ptr::operator->

    element_type* operator->() const;

Returns a pointer through which member functions of the bound object can be accessed. Never throws.

unique_ptr::release

    manual_ptr<Interface>* release();

Relinquishes ownership of the bound object. Returns an instance of manual_ptr which can be used to access the member functions of the bound object and to free it. Never throws.

unique_ptr::reset

    void reset();

If there is currently a managed object, this member frees it using delete.

unique_ptr::reset

    template<typename T>
    void reset(T* t);
Template Parameters
T-

A class implementing Interface. The type T must be complete and have an accessible destructor, but need not be complete at the point t is freed.

If there is currently a managed object, frees it as if by calling reset(). Next, takes ownership of the given object. Never throws

unique_ptr::operator unspecified-bool-type()

    operator unspecified-bool-type() const;

Returns an unspecified value which, when used in boolean contexts, is equivalent to get() != 0. Never throws.

unique_ptr::swap

    void swap(unique_ptr& p);

Exchanges the content of this instance of unique_ptr with that of the given instance. Never throws.


Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah