Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::movelib::unique_ptr< T, D > Class Template Reference

A unique pointer is an object that owns another object and manages that other object through a pointer. More...

#include <unique_ptr.hpp>

Public Types

typedef D deleter_type
 

Public Member Functions

typedef BOOST_MOVE_SEEDOC (pointer_type_obtainer::type) pointer
 If the type remove_reference<D>::type::pointer exists, then it shall be a synonym for remove_reference<D>::type::pointer. More...
 
typedef BOOST_MOVE_SEEDOC (bmupmu::remove_extent< T >::type) element_type
 If T is an array type, then element_type is equal to T. More...
 
BOOST_CONSTEXPR unique_ptr () BOOST_NOEXCEPT
 Requires: D shall satisfy the requirements of DefaultConstructible, and that construction shall not throw an exception. More...
 
BOOST_CONSTEXPR unique_ptr (BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
 Effects: Same as unique_ptr() (default constructor). More...
 
template<class Pointer >
 unique_ptr (Pointer p) BOOST_NOEXCEPT
 Requires: D shall satisfy the requirements of DefaultConstructible, and that construction shall not throw an exception. More...
 
template<class Pointer >
 unique_ptr (Pointer p, BOOST_MOVE_SEEDOC(deleter_arg_type1) d1) BOOST_NOEXCEPT
 The signature of this constructor depends upon whether D is a reference type. More...
 
 unique_ptr (BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), BOOST_MOVE_SEEDOC(deleter_arg_type1) d1) BOOST_NOEXCEPT
 Effects: Same effects as template<class Pointer> unique_ptr(Pointer p, deleter_arg_type1 d1) and additionally get() == nullptr More...
 
template<class Pointer >
 unique_ptr (Pointer p, BOOST_MOVE_SEEDOC(deleter_arg_type2) d2) BOOST_NOEXCEPT
 The signature of this constructor depends upon whether D is a reference type. More...
 
 unique_ptr (BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), BOOST_MOVE_SEEDOC(deleter_arg_type2) d2) BOOST_NOEXCEPT
 Effects: Same effects as template<class Pointer> unique_ptr(Pointer p, deleter_arg_type2 d2) and additionally get() == nullptr More...
 
 unique_ptr (BOOST_RV_REF(unique_ptr) u) BOOST_NOEXCEPT
 Requires: If D is not a reference type, D shall satisfy the requirements of MoveConstructible. More...
 
template<class U , class E >
 unique_ptr (BOOST_RV_REF_BEG unique_ptr< U, E > BOOST_RV_REF_END u) BOOST_NOEXCEPT
 Requires: If E is not a reference type, construction of the deleter from an rvalue of type E shall be well formed and shall not throw an exception. More...
 
 ~unique_ptr ()
 Requires: The expression get_deleter()(get()) shall be well formed, shall have well-defined behavior, and shall not throw exceptions. More...
 
unique_ptroperator= (BOOST_RV_REF(unique_ptr) u) BOOST_NOEXCEPT
 Requires: If D is not a reference type, D shall satisfy the requirements of MoveAssignable and assignment of the deleter from an rvalue of type D shall not throw an exception. More...
 

Detailed Description

template<class T, class D = default_delete<T>>
class boost::movelib::unique_ptr< T, D >

A unique pointer is an object that owns another object and manages that other object through a pointer.

More precisely, a unique pointer is an object u that stores a pointer to a second object p and will dispose of p when u is itself destroyed (e.g., when leaving block scope). In this context, u is said to own p.

The mechanism by which u disposes of p is known as p's associated deleter, a function object whose correct invocation results in p's appropriate disposition (typically its deletion).

Let the notation u.p denote the pointer stored by u, and let u.d denote the associated deleter. Upon request, u can reset (replace) u.p and u.d with another pointer and deleter, but must properly dispose of its owned object via the associated deleter before such replacement is considered completed.

Additionally, u can, upon request, transfer ownership to another unique pointer u2. Upon completion of such a transfer, the following postconditions hold:

  • u2.p is equal to the pre-transfer u.p,
  • u.p is equal to nullptr, and
  • if the pre-transfer u.d maintained state, such state has been transferred to u2.d.

As in the case of a reset, u2 must properly dispose of its pre-transfer owned object via the pre-transfer associated deleter before the ownership transfer is considered complete.

Each object of a type U instantiated from the unique_ptr template specified in this subclause has the strict ownership semantics, specified above, of a unique pointer. In partial satisfaction of these semantics, each such U is MoveConstructible and MoveAssignable, but is not CopyConstructible nor CopyAssignable. The template parameter T of unique_ptr may be an incomplete type.

The uses of unique_ptr include providing exception safety for dynamically allocated memory, passing ownership of dynamically allocated memory to a function, and returning dynamically allocated memory from a function.

If T is an array type (e.g. unique_ptr<MyType[]>) the interface is slightly altered:

  • Pointers to types derived from T are rejected by the constructors, and by reset.
  • The observers operator* and operator-> are not provided.
  • The indexing observer operator[] is provided.
Template Parameters
TProvides the type of the stored pointer.
DThe deleter type:
  • The default type for the template parameter D is default_delete. A client-supplied template argument D shall be a function object type, lvalue-reference to function, or lvalue-reference to function object type for which, given a value d of type D and a value ptr of type unique_ptr<T, D>::pointer, the expression d(ptr) is valid and has the effect of disposing of the pointer as appropriate for that deleter.
  • If the deleter's type D is not a reference type, D shall satisfy the requirements of Destructible.
  • If the type remove_reference<D>::type::pointer exists, it shall satisfy the requirements of NullablePointer.

Member Typedef Documentation

template<class T, class D = default_delete<T>>
typedef D boost::movelib::unique_ptr< T, D >::deleter_type

Constructor & Destructor Documentation

template<class T, class D = default_delete<T>>
BOOST_CONSTEXPR boost::movelib::unique_ptr< T, D >::unique_ptr ( )
inline

Requires: D shall satisfy the requirements of DefaultConstructible, and that construction shall not throw an exception.

Effects: Constructs a unique_ptr object that owns nothing, value-initializing the stored pointer and the stored deleter.

Postconditions: get() == nullptr. get_deleter() returns a reference to the stored deleter.

Remarks: If this constructor is instantiated with a pointer type or reference type for the template argument D, the program is ill-formed.

References boost::BOOST_STATIC_ASSERT(), and boost::program_options::value().

template<class T, class D = default_delete<T>>
BOOST_CONSTEXPR boost::movelib::unique_ptr< T, D >::unique_ptr ( BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)  )
inline

Effects: Same as unique_ptr() (default constructor).

References boost::BOOST_STATIC_ASSERT(), and boost::program_options::value().

template<class T, class D = default_delete<T>>
template<class Pointer >
boost::movelib::unique_ptr< T, D >::unique_ptr ( Pointer  p)
inlineexplicit

Requires: D shall satisfy the requirements of DefaultConstructible, and that construction shall not throw an exception.

Effects: Constructs a unique_ptr which owns p, initializing the stored pointer with p and value initializing the stored deleter.

Postconditions: get() == p. get_deleter() returns a reference to the stored deleter.

Remarks: If this constructor is instantiated with a pointer type or reference type for the template argument D, the program is ill-formed. This constructor shall not participate in overload resolution unless:

  • If T is not an array type and Pointer is implicitly convertible to pointer.
  • If T is an array type and Pointer is a more CV qualified pointer to element_type.

References boost::BOOST_STATIC_ASSERT(), and boost::program_options::value().

template<class T, class D = default_delete<T>>
template<class Pointer >
boost::movelib::unique_ptr< T, D >::unique_ptr ( Pointer  p,
BOOST_MOVE_SEEDOC(deleter_arg_type1)  d1 
)
inline

The signature of this constructor depends upon whether D is a reference type.

  • If D is non-reference type A, then the signature is unique_ptr(pointer p, const A& d).
  • If D is an lvalue-reference type A&, then the signature is unique_ptr(pointer p, A& d).
  • If D is an lvalue-reference type const A&, then the signature is unique_ptr(pointer p, const A& d).

Requires: Either

  • D is not an lvalue-reference type and d is an lvalue or const rvalue. D shall satisfy the requirements of CopyConstructible, and the copy constructor of D shall not throw an exception. This unique_ptr will hold a copy of d.
  • D is an lvalue-reference type and d is an lvalue. the type which D references need not be CopyConstructible nor MoveConstructible. This unique_ptr will hold a D which refers to the lvalue d.

Effects: Constructs a unique_ptr object which owns p, initializing the stored pointer with p and initializing the deleter as described above.

Postconditions: get() == p. get_deleter() returns a reference to the stored deleter. If D is a reference type then get_deleter() returns a reference to the lvalue d.

Remarks: This constructor shall not participate in overload resolution unless:

  • If T is not an array type and Pointer is implicitly convertible to pointer.
  • If T is an array type and Pointer is a more CV qualified pointer to element_type.

References boost::BOOST_STATIC_ASSERT().

template<class T, class D = default_delete<T>>
boost::movelib::unique_ptr< T, D >::unique_ptr ( BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)  ,
BOOST_MOVE_SEEDOC(deleter_arg_type1)  d1 
)
inline

Effects: Same effects as template<class Pointer> unique_ptr(Pointer p, deleter_arg_type1 d1) and additionally get() == nullptr

template<class T, class D = default_delete<T>>
template<class Pointer >
boost::movelib::unique_ptr< T, D >::unique_ptr ( Pointer  p,
BOOST_MOVE_SEEDOC(deleter_arg_type2)  d2 
)
inline

The signature of this constructor depends upon whether D is a reference type.

  • If D is non-reference type A, then the signature is unique_ptr(pointer p, A&& d).
  • If D is an lvalue-reference type A&, then the signature is unique_ptr(pointer p, A&& d).
  • If D is an lvalue-reference type const A&, then the signature is unique_ptr(pointer p, const A&& d).

Requires: Either

  • D is not an lvalue-reference type and d is a non-const rvalue. D shall satisfy the requirements of MoveConstructible, and the move constructor of D shall not throw an exception. This unique_ptr will hold a value move constructed from d.
  • D is an lvalue-reference type and d is an rvalue, the program is ill-formed.

Effects: Constructs a unique_ptr object which owns p, initializing the stored pointer with p and initializing the deleter as described above.

Postconditions: get() == p. get_deleter() returns a reference to the stored deleter. If D is a reference type then get_deleter() returns a reference to the lvalue d.

Remarks: This constructor shall not participate in overload resolution unless:

  • If T is not an array type and Pointer is implicitly convertible to pointer.
  • If T is an array type and Pointer is a more CV qualified pointer to element_type.

References boost::BOOST_STATIC_ASSERT().

template<class T, class D = default_delete<T>>
boost::movelib::unique_ptr< T, D >::unique_ptr ( BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)  ,
BOOST_MOVE_SEEDOC(deleter_arg_type2)  d2 
)
inline

Effects: Same effects as template<class Pointer> unique_ptr(Pointer p, deleter_arg_type2 d2) and additionally get() == nullptr

template<class T, class D = default_delete<T>>
boost::movelib::unique_ptr< T, D >::unique_ptr ( BOOST_RV_REF(unique_ptr< T, D >)  u)
inline

Requires: If D is not a reference type, D shall satisfy the requirements of MoveConstructible.

Construction of the deleter from an rvalue of type D shall not throw an exception.

Effects: Constructs a unique_ptr by transferring ownership from u to *this. If D is a reference type, this deleter is copy constructed from u's deleter; otherwise, this deleter is move constructed from u's deleter.

Postconditions: get() yields the value u.get() yielded before the construction. get_deleter() returns a reference to the stored deleter that was constructed from u.get_deleter(). If D is a reference type then get_deleter() and u.get_deleter() both reference the same lvalue deleter.

template<class T, class D = default_delete<T>>
template<class U , class E >
boost::movelib::unique_ptr< T, D >::unique_ptr ( BOOST_RV_REF_BEG unique_ptr< U, E > BOOST_RV_REF_END  u)
inline

Requires: If E is not a reference type, construction of the deleter from an rvalue of type E shall be well formed and shall not throw an exception.

Otherwise, E is a reference type and construction of the deleter from an lvalue of type E shall be well formed and shall not throw an exception.

Remarks: This constructor shall not participate in overload resolution unless:

  • unique_ptr<U, E>::pointer is implicitly convertible to pointer,
  • U is not an array type, and
  • either D is a reference type and E is the same type as D, or D is not a reference type and E is implicitly convertible to D.

Effects: Constructs a unique_ptr by transferring ownership from u to *this. If E is a reference type, this deleter is copy constructed from u's deleter; otherwise, this deleter is move constructed from u's deleter.

Postconditions: get() yields the value u.get() yielded before the construction. get_deleter() returns a reference to the stored deleter that was constructed from u.get_deleter().

References boost::BOOST_STATIC_ASSERT().

template<class T, class D = default_delete<T>>
boost::movelib::unique_ptr< T, D >::~unique_ptr ( )
inline

Requires: The expression get_deleter()(get()) shall be well formed, shall have well-defined behavior, and shall not throw exceptions.

Effects: If get() == nullpt1r there are no effects. Otherwise get_deleter()(get()).

Note: The use of default_delete requires T to be a complete type

References boost::move_upd::unique_ptr_data< P, D, bool >::deleter(), and boost::move_upd::unique_ptr_data< P, D, bool >::m_p.

Member Function Documentation

template<class T, class D = default_delete<T>>
typedef boost::movelib::unique_ptr< T, D >::BOOST_MOVE_SEEDOC ( pointer_type_obtainer::type  )

If the type remove_reference<D>::type::pointer exists, then it shall be a synonym for remove_reference<D>::type::pointer.

Otherwise it shall be a synonym for T*.

template<class T, class D = default_delete<T>>
typedef boost::movelib::unique_ptr< T, D >::BOOST_MOVE_SEEDOC ( bmupmu::remove_extent< T >::type  )

If T is an array type, then element_type is equal to T.

Otherwise, if T is a type in the form U[], element_type is equal to U.

template<class T, class D = default_delete<T>>
unique_ptr& boost::movelib::unique_ptr< T, D >::operator= ( BOOST_RV_REF(unique_ptr< T, D >)  u)
inline

Requires: If D is not a reference type, D shall satisfy the requirements of MoveAssignable and assignment of the deleter from an rvalue of type D shall not throw an exception.

Otherwise, D is a reference type; remove_reference<D>::type shall satisfy the CopyAssignable requirements and assignment of the deleter from an lvalue of type D shall not throw an exception.

Effects: Transfers ownership from u to *this as if by calling reset(u.release()) followed by get_deleter() = std::forward<D>(u.get_deleter()).

Returns: *this.

References boost::move_upd::unique_ptr_data< P, D, bool >::deleter().


The documentation for this class was generated from the following file: