unique_ptrunique_ptr
The class template unique_ptr is a Smart Interface Pointer with the following characteristics:
unique_ptr type at any given time.
unique_ptr may be passed to and returned from functions, transfering ownership from the caller to the function in the first case, and from the function to the caller in the second.
unique_ptr may neither be assigned to nor be used to initialize a second instance of a specialization of the template.
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.
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);
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);
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.
<boost/interfaces/unique_ptr.hpp>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
unique_ptr| Interface | - |
An interface defined using the Interface Definition Language (IDL). |
unique_ptr::element_typetypedef 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_ptrunique_ptr();
Constructs an empty instance of unique_ptr. Never throws.
template<typename T> explicit unique_ptr(T* t);
| T | - |
A class implementing |
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::releasemanual_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::resettemplate<typename T> void reset(T* t);
| T | - |
A class implementing |
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.
Revised 16 Jan, 2005
© Copyright Jonathan Turkanis, 2005
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah