<memory>
addressof
· allocate_shared
· allocator
· allocator_arg
· allocator_arg_t
· allocator_traits
· auto_ptr
· auto_ptr_ref
· bad_weak_ptr
· const_pointer_cast
· default_delete
· default_delete<Ty>
· dynamic_pointer_cast
· enable_shared_from_this
· get_deleter
· get_temporary_buffer
· hash
· make_shared
· operator!==
· operator==
· operator<
· operator>
· operator<=
· operator>=
· operator<<
· owner_less
· pointer_traits
· raw_storage_iterator
· shared_ptr
· static_pointer_cast
· swap
· uninitialized_copy
· uninitialized_copy_n
· uninitialized_fill
· uninitialized_fill_n
· unique_ptr
· unique_ptr<Ty, Del>
· uses_allocator
· weak_ptr
atomic_compare_exchange_strong
· atomic_compare_exchange_strong_explicit
· atomic_compare_exchange_weak
· atomic_compare_exchange_weak_explicit
· atomic_exchange
· atomic_exchange_explicit
· atomic_is_lock_free
· atomic_load
· atomic_load_explicit
· atomic_store
· atomic_store_explicit
declare_no_pointers
· declare_reachable
· get_pointer_safety
· pointer_safety
· undeclare_no_pointers
· undeclare_reachable
Include the STL
standard header <memory>
to define numerous classes, operators, and templates that help
manage memory.
namespace std { // TEMPLATE ALLOCATORS template<class Ty> class allocator; template<> class allocator<void>; template<class OutIt, class Ty> class raw_storage_iterator; template<class Ty> class auto_ptr; template<class Ty> class auto_ptr_ref; template<class Ty> bool operator==(const allocator<Ty>& left, const allocator<Ty>& right); template<class Ty> bool operator!=(const allocator<Ty>& left, const allocator<Ty>& right); // TEMPLATE FUNCTIONS template<class Ty> Ty *addressof(Ty& val) noexcept; [added with C++11] template<class Ty> pair<Ty *, ptrdiff_t> get_temporary_buffer(ptrdiff_t count) noexcept; template<class Ty> void return_temporary_buffer(Ty *pbuf); template<class InIt, class FwdIt> FwdIt uninitialized_copy(InIt first, InIt last, FwdIt dest); template<class InIt, class Diff, class FwdIt> FwdIt uninitialized_copy_n(InIt first, Diff count, FwdIt dest); [added with C++11] template<class FwdIt, class Ty> void uninitialized_fill(FwdIt first, FwdIt last, const Ty& val); template<class FwdIt, class Size, class Ty> FwdIt uninitialized_fill_n(FwdIt first, Size count, const Ty& val); // TEMPLATE UNIQUE POINTERS template<class Ty> [added with C++11] struct default_delete; template<class Ty> [added with C++11] struct default_delete<Ty[]>; template<class Ty, class Del> [added with C++11] class unique_ptr; template<class Ty, class Del> [added with C++11] class unique_ptr<Ty[], Del>; template<class Ty, class Del> [added with C++11] class hash<unique_ptr<Ty, Del>> { size_t operator()(unique_ptr<Ty, Del> val) const; }; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator==(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator==(const unique_ptr<Ty, Del>& left, nullptr_t) noexcept; template<class Ty, class Del> [added with C++11] bool operator==(nullptr_t, const unique_ptr<Ty&, Delgt;& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator!=(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator!=(const unique_ptr<Ty, Del>& left, nullptr_t) noexcept; template<class Ty, class Del> [added with C++11] bool operator!=(nullptr_t, const unique_ptr<Ty&, Delgt;& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator<(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator<(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator<(nullptr_t, const unique_ptr<Ty&, Delgt;& right); template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator>=(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator>=(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator>=(nullptr_t, const unique_ptr<Ty2, Delgt;& right); template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator>(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator>(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator>(nullptr_t, const unique_ptr<Ty&, Delgt;& right); template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator<=(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator<=(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator<=(nullptr_t, const unique_ptr<Ty&, Delgt;& right); template<class Ty, class Del> [added with C++11] void swap(unique_ptr<Ty, Del>& left, unique_ptr<Ty, Del>& right) noexcept; // TEMPLATE SHARED POINTERS template<class Ty> class shared_ptr; template<class Ty> [added with C++11] class hash<shared_ptr<Ty>> { size_t operator()(shared_ptr<Ty> val) const; }; template<class Ty> bool atomic_is_lock_free(const shared_ptr<Ty> *Ptr); template<class Ty> shared_ptr<Ty> atomic_load(const shared_ptr<Ty> *Ptr); template<class Ty> shared_ptr<Ty> atomic_load_explicit(const shared_ptr<Ty> *Ptr, memory_order Order); template<class Ty> void atomic_store(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other); template<class Ty> void atomic_store_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other, memory_order Order); template<class Ty> shared_ptr<Ty> atomic_exchange(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other); template<class Ty> shared_ptr<Ty> atomic_exchange_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other, memory_order Order); template<class Ty> bool atomic_compare_exchange_weak(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value); template<class Ty> bool atomic_compare_exchange_weak_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value, memory_order Order1, memory_order Order2); template<class Ty> bool atomic_compare_exchange_strong(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value); template<class Ty> bool atomic_compare_exchange_strong_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value, memory_order Order1, memory_order Order2); template<class Ty1, class Ty2> bool operator==(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator==(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator==(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Ty2> bool operator!=(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator!=(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator!=(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Ty2> bool operator<(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator<(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator<(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Ty2> [added with C++11] bool operator>=(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator>=(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator>=(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Ty2> [added with C++11] bool operator>(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator>(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator>(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Ty2> [added with C++11] bool operator<=(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator<=(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator<=(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Elem, class Tr, class Ty> basic_ostream<Elem, Tr>& operator<<( basic_ostream<Elem, Tr>& out, const shared_ptr<Ty>& sp); template<class Ty> void swap(shared_ptr<Ty>& left, shared_ptr<Ty>& right) noexcept; template<class D, class Ty> D *get_deleter(const shared_ptr<Ty>& sp); // TEMPLATE WEAK POINTERS template<class Ty> class weak_ptr; //template<class Ty1, class Ty2> [removed with C++11] // bool operator<(const weak_ptr<Ty1>& left, // const weak_ptr<Ty2>& right); template<class Ty> void swap(weak_ptr<Ty>& left, weak_ptr<Ty>& right) noexcept; // UTILITY TEMPLATE CLASSES template<class Ty> class enable_shared_from_this; class bad_weak_ptr; struct allocator_arg_t; constexpr allocator_arg_t allocator_arg = allocator_arg_t(); template<class Ptr> struct pointer_traits; [added with C++11] template<class Alloc> struct allocator_traits; [added with C++11] template<class Ty, class Alloc> struct uses_allocator; [added with C++11] // TEMPLATE FUNCTIONS template<class Ty, class Other> shared_ptr<Ty> const_pointer_cast(const shared_ptr<Other>& sp); template<class Ty, class Other> shared_ptr<Ty> dynamic_pointer_cast(const shared_ptr<Other>& sp); template<class Ty, class Other> shared_ptr<Ty> static_pointer_cast(const shared_ptr<Other>& sp); template<class Ty, class... Types> shared_ptr<Ty> make_shared(Types&&... args); [added with C++11] template<class Ty, class Alloc, class... Types> shared_ptr<Ty> allocate_shared(Alloc al, Types&&... args); [added with C++11] // FUNCTIONS void *align(size_t alignment, size_t size, void *& ptr, size_t& space); [added with C++11] namespace tr1 { using std::allocate_shared; using std::bad_weak_ptr; [added with C++11] using std::const_pointer_cast; using std::dynamic_pointer_cast; using std::enable_shared_from_this; using std::get_deleter; using std::hash; using std::shared_ptr; using std::static_pointer_cast; using std::unique_ptr; using std::weak_ptr; } // namespace tr1 // FUNCTORS template<class Ty> [added with C++11] struct owner_less; // not defined template<class Ty> [added with C++11] struct owner_less<shared_ptr<Ty> >; template<class Ty> [added with C++11] struct owner_less<weak_ptr<Ty> >; // POINTER SAFETY enum pointer_safety { [added with C++11] relaxed, preferred, strict }; void declare_reachable(void *ptr); [added with C++11] template<class Ty> _Ty *undeclare_reachable(void *ptr); [added with C++11] void declare_no_pointers(char *ptr, size_t size); [added with C++11] void undeclare_no_pointers(char *ptr, size_t size); [added with C++11] pointer_safety get_pointer_safety() noexcept; [added with C++11] } // namespace std
addressof
template<class Ty> Ty *addressof(Ty& val) noexcept;
The template function returns the address of val
.
align
void *align(size_t alignment, size_t size, void *& ptr, size_t& space); [added with C++11]
The function determines whether space
bytes of storage
aligned to alignment
(a power of two bytes) can be located
within the size
-byte buffer beginning at ptr
.
If so, it updates ptr
to point just past the first available
instance of such aligned storage, reduces space
to the number
of bytes remaining after the aligned storage, and returns the address of
the beginning of the aligned storage. If not, it leaves ptr
and space
unchanged and returns a null pointer.
allocate_shared
template<class Ty, class Alloc, class... Types> shared_ptr<Ty> allocate_shared(Alloc al, Types&&... args); [added with C++11]
The function creates the object shared_ptr<Ty>ptr
, where
ptr
is a pointer to Ty(args...)
as allocated and constructed by
al
.
allocator
template<class Ty> class allocator { public: typedef Ty value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef Ty *pointer; typedef const Ty *const_pointer; typedef void *void_pointer; [added with C++11] typedef const void *const_void_pointer; [added with C++11] typedef Ty& reference; typedef const Ty& const_reference; Ty *address(reference val) const noexcept; const Ty *address(const_reference val) const noexcept; template<class Other> struct rebind; allocator() throw(); template<class Other> allocator(const allocator<Other>& right) throw(); template<class Other> allocator& operator=(const allocator<Other>& right); pointer allocate(size_type count, typename allocator<void>::const_pointer *hint = 0); void deallocate(pointer ptr, size_type count); void construct(Ty *ptr, const Ty& val); template<class Uty, class Types> void construct(Uty *ptr, Types&&... args); [added with C++11] void destroy(Ty *ptr); template<class Uty> void destroy(Uty *ptr); [added with C++11] size_type max_size() const throw(); };
The template class describes an object that manages
storage allocation and freeing for arrays of objects of non-const,
non-reference, object type Ty
.
An object of class allocator
is the default
allocator object
specified in the constructors for several
container template classes in the Standard C++ library.
Template class allocator
supplies several
type definitions that are rather pedestrian,
but another class with the same members
might choose more interesting alternatives.
Constructing a container with an allocator object of such a class
gives individual control over allocation and freeing
of elements controlled by that container.
For example, an allocator object might allocate storage on a private heap. Or it might allocate storage requiring nonstandard pointers to access the allocated objects. Hence, a class that allocates storage using an allocator object should use these types religiously (as do the containers in the Standard C++ library) for declaring pointer objects, for allocating storage, and for freeing storage.
Thus, an allocator defines the types (among others):
pointer
-- behaves like a pointer to Ty
const_pointer
-- behaves like a
const pointer to Ty
void_pointer
-- behaves like a pointer to void
const_void_pointer
-- behaves like a
pointer to const void
These types specify the form that pointers take for allocated elements.
Note that allocator::pointer
is not necessarily a
raw pointer (an object that can be
declared as Ty *
) for all allocator objects, even though
it has this obvious definition for class allocator
.
You can convert any of these pointer types to raw pointers, but you
cannot necessarily convert raw pointers back to these pointer types.
The set of operations permissible for allocator pointers is described
by the template class
pointer_traits.
allocator::address
Ty *address(reference val) const noexcept; const Ty *address(const_reference val) const noexcept;
The member functions return the address of val
,
as a raw pointer.
allocator::allocate
pointer allocate(size_type count, typename allocator<void>::const_pointer *hint = 0);
The member function allocates storage for
an array of count
elements of type Ty
, by calling
operator new(count)
.
It returns a pointer to the allocated object.
The hint
argument helps some allocators
in improving locality of reference -- a valid choice
is the address of an object earlier allocated by the same allocator
object, and not yet deallocated.
A null pointer argument is taken as no hint.
allocator::allocator
allocator() throw(); template<class Other> allocator(const allocator<Other>& right) throw();
The constructor does nothing. In general, however, an allocator object constructed from another allocator object should compare equal to it (and hence permit intermixing of object allocation and freeing between the two allocator objects).
allocator::const_pointer
typedef const Ty *const_pointer;
The pointer type describes an object ptr
that can
designate, via the expression *ptr
, any const object
that an object of template class
allocator
can allocate.
allocator::const_reference
typedef const Ty& const_reference;
The reference type describes a reference to a const object.
allocator::const_void_pointer
typedef const void *const_void_pointer; [added with C++11]
The pointer type describes an object ptr
that can
represent any object of type const_pointer
,
but does not designate any object.
allocator::construct
void construct(Ty *ptr, const Ty& val); template<class Uty, class Types> void construct(Uty *ptr, Types&&... args); [added with C++11]
The first member function constructs an object of type Ty
at ptr
by evaluating the placement
new
expression new ((void *)ptr) Ty(val)
.
The second member function behaves the same as the first, but can
construct an object of any type Uty
, with any constructor
arguments Types&&...
.
allocator::deallocate
void deallocate(pointer ptr, size_type count);
The member function frees storage for
the array of count
objects of type
Ty
beginning at ptr
, by calling
operator delete(ptr)
.
The pointer ptr
must have been earlier returned by a call to
allocate
for an allocator
object that compares equal to *this
, allocating an array object
of the same size and type.
The member function throws nothing.
allocator::destroy
void destroy(Ty *ptr); template<class Uty> void destroy(Uty *ptr); [added with C++11]
The first member function destroys the object
designated by ptr
,
by calling the destructor ptr->Ty::~Ty()
.
The second member function behaves the same as the first, but can
destroy an object of any type Uty
.
allocator::difference_type
typedef ptrdiff_t difference_type;
The signed integer type describes an object that can represent the
difference between the addresses of any two elements in a sequence
that an object of template class allocator
can allocate.
allocator::max_size
size_type max_size() const throw();
The member function returns the length of the longest sequence
of elements of type Ty
that an object of class
allocator
might be able to allocate.
allocator::operator=
template<class Other> allocator& operator=(const allocator<Other>& right);
The template assignment operator does nothing. In general, however, an allocator object assigned to another allocator object should compare equal to it (and hence permit intermixing of object allocation and freeing between the two allocator objects).
allocator::pointer
typedef Ty *pointer;
The pointer type describes an object ptr
that can
designate, via the expression *ptr
, any object
that an object of template class
allocator
can allocate.
allocator::rebind
template<class Other> struct rebind { typedef allocator<Other> other; };
The member template class defines the type
other
.
Its sole purpose is to provide the type name allocator<Other>
given the type name allocator<Ty>
.
For example, given an allocator object al
of type
A
, you can allocate an object of type
Other
with the expression:
A::rebind<Other>::other(al).allocate(1, (Other *)0)
Or, you can simply name its pointer type by writing the type:
A::rebind<Other>::other::pointer
allocator::reference
typedef Ty& reference;
The reference type describes an object that can
designate any object that an object of template class
allocator
can allocate.
allocator::size_type
typedef size_t size_type;
The unsigned integer type describes an object that can represent
the length of any sequence that an object of template class
allocator
can allocate.
allocator::value_type
typedef Ty value_type;
The type is a synonym for the template parameter Ty
.
allocator::void_pointer
typedef void *void_pointer; [added with C++11]
The pointer type describes an object ptr
that can
represent any object of type pointer
,
but does not designate any object.
allocator<void>
template<> class allocator<void> { typedef void *pointer; typedef const void *const_pointer; typedef void value_type; template<class Other> struct rebind; allocator() throw(); template<class Other> allocator(const allocator<Other>) throw(); template<class Other> allocator<void>& operator=(const allocator<Other>); };
The class explicitly specializes template class
allocator for type void.
Its constructors and assignment operator behave the same as for the
template class, but it defines only the types
const_pointer
,
pointer
,
value_type
,
and the nested template class
rebind
.
allocator_arg
constexpr allocator_arg_t allocator_arg = allocator_arg_t();
The object is used as a function argument to match the parameter type
allocator_arg_t
.
allocator_arg_t
struct allocator_arg_t {};
The struct is used as a function parameter to indicate that the function should expect an allocator argument to follow.
allocator_traits
template<class Alloc> class allocator_traits { [added with C++11] public: typedef Alloc allocator_type; typedef typename Alloc::value_type value_type; typedef T1 pointer; typedef T2 const_pointer; typedef T3 void_pointer; typedef T4 const_void_pointer; typedef T5 size_type; typedef T6 difference_type; typedef T7 propagate_on_container_copy_assignment; typedef T8 propagate_on_container_move_assignment; typedef T9 propagate_on_container_swap; template<class Other> using rebind_alloc = T10; template<class Other> using rebind_traits = allocator_traits<rebind_alloc<Other> >; static pointer allocate(Alloc& al, size_type count); static pointer allocate(Alloc& al, size_type count, typename allocator_traits<void>::const_pointer *hint); static void deallocate(Alloc& al, pointer ptr, size_type count); template<class Uty, class Types> static void construct(Alloc& al, Uty *ptr, Tyes&&... args); template<class Uty> static void destroy(Alloc& al, Uty *ptr); static size_type max_size(const Alloc& al); static Alloc select_on_container_copy_construction(const Alloc& al); };
The template class describes an object that supplements an
allocator type --
any type that describes an
allocator object used for managing
allocated storage. Specifically, for any allocator type Alloc
,
you can interrogate allocator_traits<Alloc>
to determine all the information needed by an
allocator enabled container.
This is a superset of the information supplied by the default allocator class
allocator
.
In fact, given an allocator type as simple as:
template<class Ty> struct Minimal { // minimal allocator for use with allocator_traits typedef _Ty value_type; Alloc(); template<class Uty> Alloc(const Alloc<Uty> other); Ty *allocate(size_t count); void deallocate(Ty *ptr, size_t count); };
allocator_traits<Minimal<Ty> >
can make educated guesses
about all the omitted type definitions and member functions required by
an allocator enabled container.
allocator_traits::allocator_type
typedef Alloc allocator_type;
The type is a synonym for Alloc
.
allocator_traits::allocate
static pointer allocate(Alloc& al, size_type count); static pointer allocate(Alloc& al, size_type count, typename allocator_traits<void>::const_pointer *hint);
The first static member function returns al.allocate(al)
.
The second returns al.allocate(al, hint)
,
if that expression is well formed; otherwise it
returns al.allocate(al)
.
allocator_traits::const_pointer
typedef T2 const_pointer;
The type is a synonym for the type T2
, which is
Alloc::const_pointer
if that type is well formed;
otherwise it is
pointer_traits<pointer>::rebind<const value_type>
.
allocator_traits::const_void_pointer
typedef T3 const_void_pointer;
The type is a synonym for the type T4
, which is
Alloc::const_void_pointer
if that type is well formed;
otherwise it is
pointer_traits<pointer>::rebind<const void>
.
allocator_traits::construct
template<class Uty, class Types> static void construct(Alloc& al, Uty *ptr, Tyes&&... args);
The static member function calls al.construct(ptr, args...)
,
if that expression is well formed; otherwise it evaluates
::new (static_cast<void *>(ptr)) Uty(std::forward<Types>(args)...)
.
allocator_traits::deallocate
static void deallocate(Alloc al, pointer ptr, size_type count);
The static member function calls
al.deallocate(ptr, count)
.
The member function throws nothing.
allocator_traits::destroy
template<class Uty> static void destroy(Alloc& al, Uty *ptr);
The static member function calls al.destroy(ptr)
,
if that expression is well formed; otherwise it evaluates
ptr->~Uty()
.
allocator_traits::difference_type
typedef T6 difference_type;
The type is a synonym for the type T6
, which is
Alloc::difference_type
if that type is well formed;
otherwise it is
pointer_traits<pointer>::difference_type
.
allocator_traits::max_size
static size_type max_size(const Alloc& al);
The static member function returns al.max_size()
,
if that expression is well formed; otherwise it returns
numeric_limits<size_type>::max()
.
allocator_traits::pointer
typedef T1 pointer;
The type is a synonym for the type T1
, which is
Alloc::pointer
if that type is well formed;
otherwise it is value_type *
.
allocator_traits::propagate_on_container_copy_assignment
typedef T7 propagate_on_container_copy_assignment;
The type is a synonym for the type T7
, which is
Alloc::propagate_on_container_copy_assignment
if that type is well formed;
otherwise it is false_type
.
If the type holds true, an allocator enabled container copies its stored allocator on a copy assignment.
allocator_traits::propagate_on_container_move_assignment
typedef T8 propagate_on_container_move_assignment;
The type is a synonym for the type T8
, which is
Alloc::propagate_on_container_move_assignment
if that type is well formed;
otherwise it is false_type
.
If the type holds true, an allocator enabled container copies its stored allocator on a move assignment.
allocator_traits::propagate_on_container_swap
typedef T7 propagate_on_container_swap;
The type is a synonym for the type T7
, which is
Alloc::propagate_on_container_swap
if that type is well formed;
otherwise it is false_type
.
If the type holds true, an allocator enabled container swaps its stored allocator on a swap.
allocator_traits::rebind_alloc
template<class Other> using rebind_alloc = T10;
The template alias is a synonym for the type T10
, which is
Alloc::rebind<Other>::other
if that type is well formed;
otherwise it is Alloc<Other, Types...>
,
where Alloc
is a template instantiation of the form
Alloc<Ty, Types...>
.
allocator_traits::rebind_traits
template<class Other> using rebind_traits = allocator_traits<rebind_alloc<Other> >;
The template alias is a synonym for the type
allocator_traits<rebind_alloc<Other> >
.
allocator_traits::select_on_container_copy_construction
static Alloc select_on_container_copy_construction(const Alloc& al);
The static member function returns
al.select_on_container_copy_construction()
,
if that type is well formed;
otherwise it returns al
.
An allocator enabled container copies its stored allocator on a copy construction.
allocator_traits::size_type
typedef T5 size_type;
The type is a synonym for the type T5
, which is
Alloc::size_type
if that type is well formed;
otherwise it is
make_unsigned<difference_type>::type
.
allocator_traits::value_type
typedef Alloc::value_type value_type;
The type is a synonym for Alloc::value_type
.
allocator_traits::void_pointer
typedef T3 void_pointer;
The type is a synonym for the type T3
, which is
Alloc::void_pointer
if that type is well formed;
otherwise it is
pointer_traits<pointer>::rebind<void>
.
atomic_compare_exchange_strong
template <class Ty> bool atomic_compare_exchange_strong(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value);
The function returns
atomic_compare_exchange_strong_explicit(Ptr,
Expected, Value, memory_order_seq_cst, memory_order_seq_cst)
.
Ptr
and Expected
must not be null pointers.
The function throws nothing.
atomic_compare_exchange_strong_explicit
template <class Ty> bool atomic_compare_exchange_strong_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value, memory_order Order1, memory_order Order2);
The function performs an atomic compare and exchange
operation on its arguments and returns the result. The value of order2
should not be memory_order_release
or memory_order_acq_rel
,
nor should it be stronger than the value of order1
.
Ptr
and Expected
must not be null pointers.
The function throws nothing.
atomic_compare_exchange_weak
template <class Ty> bool atomic_compare_exchange_weak(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value);
The function returns
atomic_compare_exchange_weak_explicit(Ptr,
Expected, Value, memory_order_seq_cst, memory_order_seq_cst)
.
Ptr
and Expected
must not be null pointers.
The function throws nothing.
atomic_compare_exchange_weak_explicit
template <class Ty> bool atomic_compare_exchange_weak_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> *Expected, shared_ptr<Ty> Value, memory_order Order1, memory_order Order2);
The function performs a weak atomic compare and exchange
operation on its arguments and returns the result. The value of order2
should not be memory_order_release
or memory_order_acq_rel
,
nor should it be stronger than the value of order1
.
Ptr
and Expected
must not be null pointers.
The function throws nothing.
atomic_exchange
template <class Ty> shared_ptr<Ty> atomic_exchange(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other);
The function returns
atomic_exchange_explicit(Ptr, Other)
.
Ptr
must not be a null pointer.
The function throws nothing.
atomic_exchange_explicit
template <class Ty> shared_ptr<Ty> atomic_exchange_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other, memory_order Order);
The function atomically assigns Other
to *Ptr
and
returns the value that *Ptr
had on entry. It applies the memory constraints
specified by Order
, and is a read-modify-write operation.
Ptr
must not be a null pointer.
The function throws nothing.
atomic_is_lock_free
template <class Ty> bool atomic_is_lock_free(const shared_ptr<Ty> *Ptr);
The function returns true
only if atomic operations on *Ptr
are lock free.
Ptr
must not be a null pointer.
The function throws nothing.
atomic_load
template <class Ty> shared_ptr<Ty> atomic_load(const shared_ptr<Ty> *Ptr);
The function returns
atomic_load_explicit(Ptr, memory_order_seq_cst)
.
Ptr
must not be a null pointer.
The function throws nothing.
atomic_load_explicit
template <class Ty> shared_ptr<Ty> atomic_load_explicit(const shared_ptr<Ty> *Ptr, memory_order Order);
The function atomically loads the value of *Ptr
and returns the result.
It applies the memory contraints specified by Order
. The value of Order
should not be memory_order_release
or memory_order_acq_rel
.
Ptr
must not be a null pointer.
The function throws nothing.
atomic_store
template <class Ty> void atomic_store(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other);
The function calls
atomic_store_explicit(Ptr,
Other, memory_order_seq_cst)
.
Ptr
must not be a null pointer.
The function throws nothing.
atomic_store_explicit
template <class Ty> void atomic_store_explicit(shared_ptr<Ty> *Ptr, shared_ptr<Ty> Other, memory_order Order);
The function atomically stores Other
in *Ptr
. It applies the
memory contraints specified by Order
. The value of Order
should not
be memory_order_consume
, memory_order_acquire
, or
memory_order_acq_rel
.
Ptr
must not be a null pointer.
The function throws nothing.
auto_ptr
template<class Ty> class auto_ptr { [deprecated with C++11] public: typedef Ty element_type; explicit auto_ptr(Ty *ptr = 0) throw(); auto_ptr(auto_ptr<Ty>& right) throw(); template<class Other> auto_ptr(auto_ptr<Other>& right) throw(); auto_ptr(auto_ptr_ref<Ty> right) throw(); ~auto_ptr(); template<class Other> operator auto_ptr<Other>() throw(); template<class Other> operator auto_ptr_ref<Other>() throw(); template<class Other> auto_ptr<Ty>& operator=(auto_ptr<Other>& right) throw(); auto_ptr<Ty>& operator=(auto_ptr<Ty>& right) throw(); auto_ptr<Ty>& operator=(auto_ptr_ref<Ty> right) throw(); Ty& operator*() const throw(); Ty *operator->() const throw(); Ty *get() const throw(); Ty *release() throw(); void reset(Ty *ptr = 0); };
The class describes an object that stores a pointer to an allocated object
myptr
of type Ty *
. The stored pointer must either be null or
designate an object allocated by a
new
expression.
An object constructed with a non-null pointer owns the pointer.
It transfers ownership if its stored value is assigned to another
object. (It replaces the stored value after a transfer with a null pointer.)
The destructor for auto_ptr<Ty>
deletes the allocated object if it owns it.
Hence, an object of class auto_ptr<Ty>
ensures that an allocated object is automatically deleted when
control leaves a block, even via a thrown exception.
You should not construct two auto_ptr<Ty>
objects
that own the same object.
You can pass an auto_ptr<Ty>
object by value as an
argument to a function call. You can return such an object by value as well.
(Both operations depend on the implicit construction of intermediate objects
of class auto_ptr_ref<Ty>
, by various
subtle conversion rules.) You cannot, however, reliably manage a sequence of
auto_ptr<Ty>
objects with an STL
container.
auto_ptr::auto_ptr
explicit auto_ptr(Ty *ptr = 0) throw(); auto_ptr(auto_ptr<Ty>& right) throw(); auto_ptr(auto_ptr_ref<Ty> right) throw(); template<class Other> auto_ptr(auto_ptr<Other>& right) throw();
The first constructor stores ptr
in myptr
,
the stored pointer to the allocated object.
The second constructor transfers ownership of the
pointer stored in right
, by storing
right.release()
in myptr
.
The third constructor behaves the same as the second, except
that it stores right.ref.release()
in myptr
, where ref
is the reference stored in right
.
The template constructor behaves the same as the second constructor,
provided that a pointer to Other
can be implicitly converted
to a pointer to Ty
.
auto_ptr::~auto_ptr
~auto_ptr();
The destructor evaluates the expression delete myptr
to delete the object designated by the stored pointer.
auto_ptr::element_type
typedef Ty element_type;
The type is a synonym for the template parameter Ty
.
auto_ptr::get
Ty *get() const throw();
The member function returns the stored pointer myptr
.
auto_ptr::operator=
template<class Other> auto_ptr<Ty>& operator=(auto_ptr<Other>& right) throw(); auto_ptr<Ty>& operator=(auto_ptr<>& right) throw(); auto_ptr<Ty>& operator=(auto_ptr_ref<>& right) throw();
The assignment evaluates the expression delete myptr
,
but only if the stored pointer myptr
changes as a result of the assignment.
It then transfers ownership of the pointer designated by right
, by storing
right.release()
in myptr
.
(The last assignment behaves as if right
designates the reference
it stores.) The function returns *this
.
auto_ptr::operator*
Ty& operator*() const throw();
The indirection operator returns
*get()
.
Hence, the stored pointer must not be null.
auto_ptr::operator->
Ty *operator->() const throw();
The selection operator returns
get()
,
so that the expression ap->member
behaves the same as
(ap.get())->member
, where ap
is an object
of class auto_ptr<Ty>
.
Hence, the stored pointer must not be null, and Ty
must be a class, structure, or union type with a member member
.
auto_ptr::operator auto_ptr<Other>
template<class Other> operator auto_ptr<Other>() throw();
The type cast operator returns
auto_ptr<Other>(*this)
.
auto_ptr::operator auto_ptr_ref<Other>
template<class Other> operator auto_ptr_ref<Other>() throw();
The type cast operator returns
auto_ptr_ref<Other>(*this)
.
auto_ptr::release
Ty *release() throw();
The member replaces the stored pointer myptr
with a null pointer and
returns the previously stored pointer.
auto_ptr::reset
void reset(Ty *ptr = 0);
The member function evaluates the expression delete myptr
,
but only if the stored pointer value myptr
changes as a result of function call.
It then replaces the stored pointer with ptr
.
auto_ptr_ref
template<class Ty> struct auto_ptr_ref { };
The class describes an object that stores a reference to an object of class
auto_ptr<Ty>
. It is used as a helper
class for auto_ptr<Ty>
. You should not have an occasion
to construct an auto_ptr_ref<Ty>
object directly.
bad_weak_ptr
class bad_weak_ptr [added with C++11] : public exception { public: bad_weak_ptr() noexcept; const char *what() throw(); };
The class describes an exception that can be thrown from the
shared_ptr
constructor that takes an argument
of type weak_ptr
. The member function what
returns "bad_weak_ptr"
.
const_pointer_cast
template <class Ty, class Other> [added with C++11] shared_ptr<Ty> const_pointer_cast(const shared_ptr<Other>& sp) noexcept;
The template function returns an empty shared_ptr object
if const_cast<Ty*>(sp.get())
returns a null pointer; otherwise it returns
a shared_ptr<Ty>
object that
owns the resource that is owned by sp
.
The expression const_cast<Ty*>(sp.get())
must be valid.
declare_no_pointers
void declare_no_pointers(char *ptr, size_t size); [added with C++11]
The function informs any
garbage collector
that the range of addresses [ptr, ptr + size)
no longer contain
traceable pointers.
(Any pointers to allocated storage must not be dereferenced unless
made reachable.)
The function throws nothing.
declare_reachable
void declare_reachable(void *ptr); [added with C++11]
If ptr
is not null, the function informs any
garbage collector
that ptr
is hereafter
reachable (points to valid allocated
storage).
default_delete
template<class Ty> struct default_delete { [added with C++11] constexpr default_delete() noexcept = default; template<class Other> default_delete(const default_delete<Other>&) noexcept; void operator()(Ty *ptr) const; };
The template class describes a deleter
that deletes scalar objects allocated with operator new
,
suitable for use with template class
unique_ptr
. It also has the
explicit specialization default_delete<Ty[]>
.
default_delete::default_delete
constexpr default_delete() noexcept = default; template<class Other> default_delete(const default_delete<Other>&) noexcept;
The constructors do nothing. In the second constructor, unless
is_convertible<Other *, Ty *>
holds true,
the constructor does not participate in overload resolution.
default_delete::operator()
void operator()(Ty *ptr) const;
The member operator executes delete ptr
.
Ty
must not be an incomplete type.
default_delete<Ty[]>
template<class Ty> struct default_delete<Ty[]> { [added with C++11] constexpr default_delete() noexcept = default; template<class Other> void operator()(Other *ptr) const = delete; };
The template class describes a deleter
that deletes array objects allocated with operator new[]
,
suitable for use with template class
unique_ptr<Ty[]>
. It is an
explicit specialization of the template class
default_delete
.
default_delete<Ty[]>::operator()
void operator()(Ty *ptr) const; template<class Other> void operator()(Other *ptr) const = delete;
The member operator executes delete[] ptr
.
Ty
must not be an incomplete type.
dynamic_pointer_cast
template <class Ty, class Other> [added with C++11] shared_ptr<Ty> dynamic_pointer_cast(const shared_ptr<Other>& sp) noexcept;
The template function returns an empty shared_ptr object
if dynamic_cast<Ty*>(sp.get())
returns a null pointer; otherwise it returns
a shared_ptr<Ty>
object that
owns the resource that is owned by sp
.
The expression dynamic_cast<Ty*>(sp.get())
must be valid.
enable_shared_from_this
template<class Ty> [added with C++11] class enable_shared_from_this { public: shared_ptr<Ty> shared_from_this(); shared_ptr<const Ty> shared_from_this() const; protected: constexpr enable_shared_from_this() noexcept; enable_shared_from_this(const enable_shared_from_this&) noexcept; enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept; ~enable_shared_from_this(); };
The template class can be used as a public base class to simplify creating
shared_ptr
objects that own objects of the derived type:
class derived
: public enable_shared_from_this<derived>
{
};
shared_ptr<derived> sp0 = new derived;
shared_ptr<derived> sp1 = sp0->shared_from_this();
The constructors, destructor, and assignment operator are protected to help
prevent accidental misuse. The template argument type Ty
must be the
type of the derived class.
enable_shared_from_this::shared_from_this
shared_ptr<Ty> shared_from_this(); shared_ptr<const Ty> shared_from_this() const;
The member functions each return a shared_ptr
object that
owns *(Ty*)this
.
get_deleter
template<class D, class Ty> [added with C++11] D *get_deleter(const shared_ptr<Ty>& sp) noexcept;
The template function returns a pointer to the deleter
of type D
that belongs to the shared_ptr
object sp
. If sp
has no deleter or if its deleter is
not of type D
the function returns 0.
get_pointer_safety
pointer_safety get_pointer_safety() noexcept; [added with C++11]
The function returns the type of pointer safety assumed by any automatic garbage collector.
get_temporary_buffer
template<class Ty> pair<Ty *, ptrdiff_t> get_temporary_buffer(ptrdiff_t count) noexcept;
The template function allocates storage for a sequence of at most
count
elements of type Ty
, from an unspecified
source (which may well be the standard heap used by
operator new
).
It returns a value pr
, of type
pair<Ty *, ptrdiff_t>
.
If the function allocates storage,
pr.first
designates
the allocated storage and
pr.second
is the number of elements in the longest sequence the storage can hold.
Otherwise, pr.first
is a null pointer.
hash
template<class Ty> [added with C++11] class hash<shared_ptr<Ty>> { size_t operator()(shared_ptr<Ty> val) const; }; template<class Ty, class Del> [added with C++11] class hash<unique_ptr<Ty, Del>> { size_t operator()(unique_ptr<Ty, Del> val) const; };
Each template class defines its member function as returning
a value uniquely determined by val
. In each case,
the member function defines a
hash function, suitable for mapping values
of the argument type to a distribution of index values.
make_shared
template<class Ty, class... Types> shared_ptr<Ty> make_shared(Types&&... args); [added with C++11]
The function creates the object shared_ptr<Ty>
, where
ptr
is a pointer to Ty(args...)
as allocated and constructed by
the default allocator allocator()
.
operator!=
template<class Ty> bool operator!=(const allocator<Ty>& left, const allocator<Ty>& right) throw(); template<class Ty1, class Ty2> [added with C++11] bool operator!=(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator!=(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator!=(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator!=(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator!=(const unique_ptr<Ty, Del>& left, nullptr_t) noexcept; template<class Ty, class Del> [added with C++11] bool operator!=(nullptr_t, const unique_ptr<Ty&, Delgt;& right) noexcept;
The first template operator returns false.
(All default allocators are equal.)
The remaining template operators return !(left == right)
,
if both left
and right
are defined. Otherwise, for
an argument X
of type nullptr_t
, its corresponding
term is (Ty *)0
.
operator==
template<class Ty> bool operator==(const allocator<Ty>& left, const allocator<Ty>& right) throw(); template<class Ty1, class Ty2> [added with C++11] bool operator==(const shared_ptr<Ty1>& left;, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator==(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator==(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator==(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator==(const unique_ptr<Ty, Del>& left, nullptr_t) noexcept; template<class Ty, class Del> [added with C++11] bool operator==(nullptr_t, const unique_ptr<Ty&, Delgt;& right) noexcept;
The first template operator returns true.
(All default allocators are equal.)
The remaining template operators return left.get() == right.get()
,
if both left
and right
are defined. Otherwise, for
an argument X
of type nullptr_t
, its corresponding
term is (Ty *)0
.
operator<
template<class Ty1, class Ty2> [added with C++11] bool operator<(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator<(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator<(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator<(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator<(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator<(nullptr_t, const unique_ptr<Ty&, Delgt;& right);
The template operators return
less<V>()(left.get(), right.get())
,
where V
is either Ty
or the composite pointer type of Ty1 *
and Ty2 *
.
For an argument X
of type nullptr_t
, its corresponding
term is (Ty *)0
.
operator<<
template<class Elem, class Tr, class Ty> [added with C++11] basic_ostream<Elem, Tr>& operator<<(basic_ostream<Elem, Tr>& out, shared_ptr<Ty>& sp);
The template operator returns out << sp.get()
.
operator<=
template<class Ty1, class Ty2> [added with C++11] bool operator<=(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator<=(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator<=(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator<=(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator<=(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator<(nullptr_t, const unique_ptr<Ty&, Delgt;& right);
The template operators return left.get() <= right.get()
.
For an argument X
of type nullptr_t
, its corresponding
term is (Ty *)0
.
operator>
template<class Ty1, class Ty2> [added with C++11] bool operator>(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator>(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator>(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator>(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator>(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator>(nullptr_t, const unique_ptr<Ty&, Delgt;& right);
The template operators return left.get() > right.get()
.
For an argument X
of type nullptr_t
, its corresponding
term is (Ty *)0
.
operator>=
template<class Ty1, class Ty2> [added with C++11] bool operator>=(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right) noexcept; template<class Ty> [added with C++11] bool operator>=(const shared_ptr<Ty>& left, nullptr_t) noexcept; template<class Ty> [added with C++11] bool operator>=(nullptr_t, const shared_ptr<Ty2>& right) noexcept; template<class Ty1, class Del1, class Ty2, class Del2> [added with C++11] bool operator>=(const unique_ptr<Ty1, Del1>& left, const unique_ptr<Ty2&, Del2gt;& right); template<class Ty, class Del> [added with C++11] bool operator>=(const unique_ptr<Ty, Del>& left, nullptr_t); template<class Ty, class Del> [added with C++11] bool operator>=(nullptr_t, const unique_ptr<Ty&, Delgt;& right);
The template operators return left.get() >= right.get()
.
For an argument X
of type nullptr_t
, its corresponding
term is (Ty *)0
.
owner_less
template<class Ty> [added with C++11] struct owner_less; // not defined template<class Ty> [added with C++11] struct owner_less<shared_ptr<Ty> > { bool operator()(const shared_ptr<Ty>& left, const shared_ptr<Ty>& right); bool operator()(const shared_ptr<Ty>& left, const weak_ptr<Ty>& right); bool operator()(const weak_ptr<Ty>& left, const shared_ptr<Ty>& right); }; template<class Ty> [added with C++11] struct owner_less<weak_ptr<Ty> > bool operator()(const weak_ptr<Ty>& left, const weak_ptr<Ty>& right); bool operator()(const weak_ptr<Ty>& left, const shared_ptr<Ty>& right); bool operator()(const shared_ptr<Ty>& left, const weak_ptr<Ty>& right); };
The template classes define all their member operators as returning
left.owner_before(right)
.
pointer_safety
enum pointer_safety { [added with C++11] relaxed, preferred, strict };
The scoped enum defines the values that can be returned by
get_pointer_safety()
:
relaxed
-- pointers not
safely derived (obviously
pointers to declared or allocated objects) are treated the same as those
safely derived.preferred
-- as before, but pointers not safely derived
should not be dereferenced.strict
-- pointers not safely derived might be treated
differently than those safely derived.pointer_traits
template<class Ptr> struct pointer_traits [added with C++11] { // defines traits for arbitrary pointers typedef T1 element_type; typedef Ptr pointer; typedef T2 difference_type; template<Other> using rebind = T3; static pointer pointer_to(element_type& obj); };
The template class supplies all the information needed by an object
of template class allocator_traits
to describe an allocator with pointer type Ptr
.
Such a pointervtype can be a
raw pointer of type Ty *
or a class with the properties:
template<class Ty, class... Rest> struct Ptr { // describes a pointer type usable by allocators typedef Ptr pointer; typedef T1 element_type; // optional typedef T2 difference_type; // optional template<class Other> using rebind = typename Ptr<Other, Rest...>; // optional static pointer pointer_to(element_type& obj); // optional };
pointer_traits::difference_type
typedef T2 difference_type;
The type T2
is Ptr::difference_type
if that
type exists, otherwise ptrdiff_t
.
For Ptr
a raw pointer Ty *
it is ptrdiff_t
.
pointer_traits::element_type
typedef T1 element_type;
The type T1
is Ptr::element_type
if that
type exists, otherwise Ty
.
For Ptr
a raw pointer Ty *
it is Ty
.
pointer_traits::pointer
typedef Ptr pointer;
The type is Ptr
.
pointer_traits::pointer_to
static pointer pointer_to(element_type& obj);
The static member function returns Ptr::pointer_to(obj)
,
if that function exists. Otherwise, it is not possible to convert an
arbitrary reference to a object of class Ptr
.
For Ptr
a raw pointer Ty *
it returns addressof(obj)
.
pointer_traits::rebind
template<Other> using rebind = T3;
The type T3
is Ptr::rebind<Other>
if that
type exists, otherwise Ptr<Other, Rest...>
.
For Ptr
a raw pointer Ty *
it is Other *
.
raw_storage_iterator
template<class OutIt, class Ty> class raw_storage_iterator : public iterator<output_iterator_tag, void, void, void, void> { public: explicit raw_storage_iterator(OutIt first); raw_storage_iterator<OutIt, Ty>& operator*(); raw_storage_iterator<OutIt, Ty>& operator=(const Ty& val); raw_storage_iterator<OutIt, Ty>& operator++(); raw_storage_iterator<OutIt, Ty> operator++(int); };
The class describes an output iterator
that constructs objects of type Ty
in the sequence it generates. An object of class
raw_storage_iterator<OutIt, Ty>
accesses storage through an output iterator object,
of class OutIt
, that you specify when you construct
the object. For an object first
of class OutIt
,
the expression &*first
must designate unconstructed storage for
the next object (of type Ty
) in the generated sequence.
raw_storage_iterator::operator*
raw_storage_iterator<OutIt, Ty>& operator*();
The indirection operator returns *this
(so that
operator=(const
Ty&)
can perform the actual store
in an expression such as *ptr = val
).
raw_storage_iterator::operator=
raw_storage_iterator<OutIt, Ty>& operator=(const Ty& val);
The assignment operator constructs the next object in the
output sequence using the stored iterator value first
,
by evaluating the
placement new
expression
new ((void *)&*first) Ty(val)
.
The function returns *this
.
raw_storage_iterator::operator++
raw_storage_iterator<OutIt, Ty>& operator++(); raw_storage_iterator<OutIt, Ty> operator++(int);
The first (preincrement) operator increments the stored output iterator
object, then returns *this
.
The second (postincrement) operator makes a copy of *this
,
increments the stored output iterator object, then returns
the copy.
raw_storage_iterator::raw_storage_iterator
explicit raw_storage_iterator(OutIt first);
The constructor stores first
as the output iterator
object.
return_temporary_buffer
template<class Ty> void return_temporary_buffer(Ty *pbuf);
The template function frees the storage designated by pbuf
,
which must be earlier allocated by a call to
get_temporary_buffer
.
shared_ptr
template<class Ty> class shared_ptr { [added with C++11] public: typedef Ty element_type; constexpr shared_ptr() noexcept; constexpr shared_ptr(nullptr_t); [added with C++11] shared_ptr(const shared_ptr& sp) noexcept; shared_ptr(shared_ptr&& sp) noexcept; [added with C++11] template<class Other> explicit shared_ptr(Other *ptr); template<class Other, class D> shared_ptr(Other *ptr, D dtor); template<class D> shared_ptr(nullptr_t ptr, D dtor); [added with C++11] template<class Other, class D, class A> shared_ptr(Other *ptr, D dtor, A alloc); [added with C++11] template<class D, class A> shared_ptr(nullptr_t ptr, D dtor, A alloc); [added with C++11] template<class Other> shared_ptr(const shared_ptr<Other>& sp) noexcept; template<class Other> shared_ptr(shared_ptr<Other>&& sp) noexcept; [added with C++11] template<class Other> shared_ptr(const weak_ptr<Other>& wp); template<class > shared_ptr(auto_ptr<Other>&& ap); template<class Other, class D> shared_ptr(unique_ptr<Other, D>&& up); [added with C++11] template<class Other> shared_ptr(const shared_ptr<Other>& sp, Ty *ptr) noexcept; [added with C++11] ~shared_ptr(); shared_ptr& operator=(const shared_ptr& sp) noexcept; shared_ptr& operator=(shared_ptr&& sp) noexcept; [added with C++11] template<class Other> shared_ptr& operator=(const shared_ptr<Other>& sp) noexcept; template<class Other> shared_ptr& operator=(auto_ptr<Other>&& ap); template<class Other, class D> shared_ptr& operator=(shared_ptr<Other, D>&& ap) noexcept; [added with C++11] void swap(shared_ptr& sp) noexcept; void reset() noexcept; template<class Other> void reset(Other *ptr); template<class Other, class D> void reset(Other *ptr, D dtor); template<class Other, class D, class A> void reset(Other *ptr, D dtor, A alloc); [added with C++11] Ty *get() const noexcept; Ty& operator*() const noexcept; Ty *operator->() const noexcept; long use_count() const noexcept; bool unique() const noexcept; explicit operator bool() cons noexcept; template<class Other> bool owner_before(const shared_ptr<Other>& ptr); [added with C++11] template<class Other> bool owner_before(const weak_ptr<Other>& ptr); [added with C++11] };
The template class describes an object that uses reference counting to manage resources.
Each shared_ptr
object effectively stores a pointer to the resource that it
owns or stores a null pointer. A resource can be owned by more than one
shared_ptr
object; when the last shared_ptr
object that owns
a particular resource is destroyed the resource is freed.
The template argument Ty
may be an incomplete type except as noted
for certain operand sequences.
When a shared_ptr<Ty>
object is constructed from a
resource pointer of type D*
or from a shared_ptr<D>
,
the pointer type D*
must be convertible to Ty*
. If it is
not, the code will not compile. For example:
class B {}; class D : public B {}; shared_ptr<D> sp0(new D); // okay, template parameter D and argument D* shared_ptr<D> sp1(sp0); // okay, template parameter D and argument shared_ptr<D> shared_ptr<B> sp2(new D); // okay, D* convertible to B* shared_ptr<B> sp3(sp0); // okay, template parameter B and argument shared_ptr<D> shared_ptr<B> sp4(sp2); // okay, template parameter B and argument shared_ptr<B> shared_ptr<int> sp4(new D); // error, D* not convertible to int* shared_ptr<int> sp5(sp2); // error, template parameter int and argument shared_ptr<B>
A shared_ptr
object owns a resource:
shared_ptr
object that owns that resource,weak_ptr
object that
points to that resource, oroperator=
or by calling the member function
reset
.All the shared_ptr
objects that own a single resource share a control block which holds the number
of shared_ptr
objects that own the resource, the number of
weak_ptr
objects that point to the resource, and the
deleter for that resource if it has one. A shared_ptr
object that was initialized with a null pointer has a control block; thus it is not an
empty shared_ptr.
After a shared_ptr
object releases a resource
it no longer owns that resource. After a weak_ptr
object releases a resource
it no longer points to that resource.
When the number of shared_ptr
objects that own a resource becomes zero
the resource is freed, either by deleting it or by passing its address to a
deleter, depending on how ownership of the resource was originally created. When the
number of shared_ptr
objects that own a resource is zero and the number
of weak_ptr
objects that point to that resource is zero the control block
is freed.
An empty shared_ptr object does not own any resources and has no control block.
A deleter is a function pointer or an object of a type
with a member function operator()
. Its type must be copy constructible
and its copy constructor and destructor must not throw exceptions. A deleter is bound
to a shared_ptr
object with an
operand sequence of the form ptr, dtor
.
Some functions take an operand sequence
that defines properties of the resulting shared_ptr<Ty>
or weak_ptr<Ty>
object. You can specify such an
operand sequence several ways:
ptr
-- a pointer of type Other*
to the resource to be
managed. Ty
must be a complete type. If the function fails
it evaluates the expression delete ptr
.ptr, dtor
-- a pointer of type Other*
to the
resource to be managed and a deleter for that resource.
If the function fails it calls dtor(ptr)
, which must be well
defined.ptr, dtor, alloc
-- a pointer of type Other*
to the
resource to be managed, a deleter for that resource, and an
allocator to manage any storage that must be allocated and freed.
If the function fails it calls dtor(ptr)
, which must be well
defined.sp
-- a shared_ptr<Other>
object that
owns the resource to be managed.wp
-- a weak_ptr<Other>
object that
points to the resource to be managed.ap
-- an auto_ptr<Other>
object that holds
a pointer to the resource to be managed. If the function succeeds it calls
ap.release()
; otherwise it leaves ap
unchanged.In all cases, the pointer type Other *
must be convertible to
Ty *
.
shared_ptr
and weak_ptr
objects provide limited thread-safety
guarantees. Creating new objects from existing objects is safe, and destroying
objects is safe. Other operations, including assigning from one object to
another, are not safe. To use these unsafe operations in multiple threads, either
use the various shared_ptr
atomic
operations or provide your own synchronization.
shared_ptr::element_type
typedef Ty element_type;
The type is a synonym for the template parameter Ty
.
shared_ptr::get
Ty *get() const;
The member function returns the address of the owned resource. If the object does not own a resource it returns 0.
shared_ptr::operator=
shared_ptr& operator=(const shared_ptr& sp) noexcept; shared_ptr& operator=(shared_ptr&& sp) noexcept; template<class Other> shared_ptr& operator=(const shared_ptr<Other>& sp) noexcept; template<class Other> shared_ptr& operator=(auto_ptr<Other>&& ap); template<class Other, class D> shared_ptr& operator=(unique_ptr<Other, D>&& ap) noexcept; [added with C++11]
The operators all release the resource currently
owned by *this
and assign ownership of the
resource named by the operand sequence to
*this
. If an operator fails it leaves
*this
unchanged.
shared_ptr::operator*
Ty& operator*() const noexcept;
The indirection operator returns *get()
. Hence, the stored
pointer must not be null.
shared_ptr::operator->
Ty *operator->() const noexcept;
The selection operator returns get()
, so that the
expression sp->member
behaves the same as (sp.get())->member
where sp
is an object of class shared_ptr<Ty>
.
Hence, the stored pointer must not be null, and Ty
must be a class,
structure, or union type with a member member
.
shared_ptr::operator bool
explicit operator bool() const noexcept;
The operator returns a value of a type that is convertible to bool
.
The result of the conversion to bool
is true
when
get() != 0
, otherwise false
.
shared_ptr::owner_before
template<class Other> bool owner_before(const shared_ptr<Other>& ptr); [added with C++11] template<class Other> bool owner_before(const weak_ptr<Other>& ptr); [added with C++11]
The template member function returns true if *this
is
ordered before ptr
.
shared_ptr::reset
void reset() noexcept; template<class Other> void reset(Other *ptr;); template<class Other, class D> void reset(Other *ptr, D dtor); template<class Other, class D, class A> void reset(Other *ptr, D dtor, A alloc); [added with C++11]
The member functions all release the resource currently
owned by *this
and assign ownership of the
resource named by the operand sequence to
*this
. If a member function fails it leaves
*this
unchanged.
shared_ptr::shared_ptr
constexpr shared_ptr() noexcept; constexpr shared_ptr(nullptr_t); [added with C++11] shared_ptr(const shared_ptr& sp) noexcept; shared_ptr(shared_ptr&& sp); [added with C++11] template<class Other> explicit shared_ptr(Other *ptr); template<class Other, class D> shared_ptr(Other *ptr, D dtor); template<class D> shared_ptr(nullptr_t ptr, D dtor); [added with C++11] template<class Other, class D, class A> shared_ptr(Other *ptr, D dtor, A alloc); [added with C++11] template<class D, class A> shared_ptr(nullptr_t ptr, D dtor, A alloc); [added with C++11] template<class Other> shared_ptr(const shared_ptr<Other>& sp) noexcept; template<class Other> shared_ptr(shared_ptr<Other>&& sp) noexcept; [added with C++11] template<class Other> shared_ptr(const weak_ptr<Other>& wp); template<class Other> shared_ptr(auto_ptr<Other>&& ap) noexcept; template<class Other, class D> shared_ptr(unique_ptr<Other, D>&& ap); [added with C++11] template<class Other> shared_ptr(const shared_ptr<Other>& sp, Ty *ptr) noexcept; [added with C++11]
All but the last constructor each constructs an object that owns
the resource named by the operand sequence.
The last constructor defines ptr
as a
shared pointer alias for the object
owned by sp
, and shares ownership with sp
.
The constructor shared_ptr(const weak_ptr<Other>& wp)
throws
an exception object of type bad_weak_ptr
if wp.expired()
.
Unless is_convertible<Other *, Ty *>
holds true,
the template constructor
shared_ptr(const shared_ptr<Other>& ap)
does not participate in overload resolution.
Unless is_convertible<Other *, Ty *>
holds true,
the template constructor
shared_ptr(shared_ptr<Other>&& ap)
does not participate in overload resolution.
shared_ptr::~shared_ptr
~shared_ptr();
The destructor releases the resource
owned by *this
.
shared_ptr::swap
void swap(shared_ptr& sp) noexcept;
The member function leaves the resource originally owned
by *this
subsequently owned by sp
, and the resource
originally owned by sp
subsequently owned by *this
.
The function does not change the reference counts for the two resources and
it does not throw any exceptions.
shared_ptr::unique
bool unique() const noexcept;
The member function returns true
if
no other shared_ptr
object owns the
resource that is owned by *this
, otherwise false
.
shared_ptr::use_count
long use_count() const noexcept;
The member function returns the number of shared_ptr
objects
that own the resource that is owned by *this
.
static_pointer_cast
template <class Ty, class Other> [added with C++11] shared_ptr<Ty> static_pointer_cast(const shared_ptr<Other>& sp) noexcept;
The template function returns an empty shared_ptr object
if sp
is an empty shared_ptr
object; otherwise it returns a
shared_ptr<Ty>
object that
owns the resource that is owned by sp
. The expression
static_cast<Ty*>(sp.get())
must be valid.
swap
template<class Ty> [added with C++11] void swap(shared_ptr<Ty>& left, shared_ptr<Ty>& right) noexcept; template<class Ty, class Del> [added with C++11] void swap(unique_ptr<Ty, Del>& left, unique_ptr<Ty, Del>& right) noexcept; template<class Ty> [added with C++11] void swap(weak_ptr<Ty>& left, weak_ptr<Other>& right) noexcept;
The template function calls left.swap(right)
.
undeclare_no_pointers
void undeclare_no_pointers(char *ptr, size_t size); [added with C++11]
The function informs any
garbage collector
that the range of addresses [ptr, ptr + size)
may now contain
traceable pointers.
The function throws nothing.
undeclare_reachable
template<class Ty> _Ty *undeclare_reachable(void *ptr); [added with C++11]
If ptr
is not null, the function informs any
garbage collector
that ptr
is hereafter not
reachable.
It returns a safely derived pointer
that compares equal to ptr
.
The function throws nothing.
uninitialized_copy
template<class InIt, class FwdIt> FwdIt uninitialized_copy(InIt first, InIt last, FwdIt dest);
The template function effectively executes:
while (first != last) new ((void *)&*dest++) iterator_traits<InIt>::value_type(*first++); return dest;
unless the code throws an exception. In that case, all constructed objects are destroyed and the exception is rethrown.
uninitialized_copy_n
template<class InIt, class Diff, class FwdIt> FwdIt uninitialized_copy_n(InIt first, Diff count, FwdIt dest); [added with C++11]
The template function effectively executes:
for (; 0 < count; -- count) new ((void *)&*dest++) iterator_traits<InIt>::value_type(*first++); return dest;
unless the code throws an exception. In that case, all constructed objects are destroyed and the exception is rethrown.
uninitialized_fill
template<class FwdIt, class Ty> void uninitialized_fill(FwdIt first, FwdIt last, const Ty& val);
The template function effectively executes:
while (first != last) new ((void *)&*first++) iterator_traits<FwdIt>::value_type(val);
unless the code throws an exception. In that case, all constructed objects are destroyed and the exception is rethrown.
uninitialized_fill_n
template<class FwdIt, class Size, class Ty> FwdIt uninitialized_fill_n(FwdIt first, Size count, const Ty& val);
The template function effectively executes:
while (0 < count--) new ((void *)&*first++) iterator_traits<FwdIt>::value_type(val); return (first);
unless the code throws an exception. In that case, all constructed objects are destroyed and the exception is rethrown.
unique_ptr
template<class Ty, class Del = default_delete<Ty> > struct unique_ptr { [added with C++11] typedef Ty element_type; typedef Del deleter_type; typedef T1 pointer; constexpr unique_ptr() noexcept; constexpr unique_ptr(nullptr_t) noexcept; explicit unique_ptr(pointer ptr) noexcept; unique_ptr(pointer ptr, typename conditional<is_reference<Del>::value, Del, typename add_reference<const Del>::type>::type deleter) noexcept; unique_ptr(pointer ptr, typename remove_reference<Del>::type&& deleter) noexcept; unique_ptr(unique_ptr&& right) noexcept; template<class Ty2, Class Del2> unique_ptr(unique_ptr<Ty2, Del2>&& right) noexcept; template<class Ty2> unique_ptr(auto_ptr<Ty2>&& right) noexcept; ~unique_ptr(); unique_ptr& operator=(unique_ptr&& right) noexcept; template<class Ty2, Class Del2> unique_ptr& operator=(unique_ptr<Ty, Del>&& right) noexcept; unique_ptr& operator=(nullptr_t) noexcept; void swap(unique_ptr& right) noexcept; pointer release() noexcept; void reset(pointer ptr = pointer()) noexcept; void reset(nullptr_t ptr) noexcept; pointer get() const noexcept; Ty& operator*() const; pointer operator->() const noexcept; Del& get_deleter() noexcept; const Del& get_deleter() const noexcept; explicit operator bool() const noexcept; 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 };
The template class describes an object that uniquely manages a resource.
Each unique_ptr
object effectively 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 the unique_ptr
object that owns a particular resource is destroyed the resource is freed.
A unique_ptr
object may be moved, but not copied.
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<Ty>
assumes that the resource pointed to by ptr
is allocated with
operator new
, so it can be freed by calling Del()(ptr)
.
(An explicit specialization unique_ptr<Ty[]>
manages array objects allocated with operator new[]
, and has the default deleter
default_delete<Ty[]>
.)
The stored pointer to a managed resource stored_ptr
has type
pointer
. It is Del::pointer
if defined, otherwise Ty *
. The stored deleter object
stored_deleter
should occupy no space in the object if the deleter is stateless.
Note that Del
can be a reference type.
unique_ptr::deleter_type
typedef Del deleter_type;
The type is a synonym for the template parameter Del
.
unique_ptr::element_type
typedef Ty element_type;
The type is a synonym for the template parameter Ty
.
unique_ptr::get
pointer get() const noexcept;
The member function returns stored_ptr
.
unique_ptr::get_deleter
Del& get_deleter() noexcept; const Del& get_deleter() const noexcept;
The member function returns a reference to stored_deleter
.
unique_ptr::operator=
unique_ptr& operator=(unique_ptr&& right) noexcept; template<class Ty2, Class Del2> unique_ptr& operator=(unique_ptr<Ty, Del>&& right) noexcept; unique_ptr& operator=(nullptr_t) noexcept;
The first two member operators call reset(right.release())
and move right.stored_deleter
to stored_deleter
,
then return *this
.
The third member operator calls reset()
,
then returns *this
.
Unless is_convertible<Ty2 *, Ty *>
holds true,
and is_array<Ty2>
holds false,
the template operator
unique_ptr& operator=(unique_ptr<Ty, Del>&& right)
does not participate in overload resolution.
unique_ptr::operator*
Ty& operator*() const;
The member function returns *stored_ptr
.
unique_ptr::operator->
pointer operator->() const noexcept;
The member function returns stored_ptr
.
unique_ptr::operator bool
explicit operator bool() const noexcept;
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
.
unique_ptr::pointer
typedef T1 pointer;
The type is a synonym for Del::pointer
if defined, otherwise Ty *
.
unique_ptr::release
pointer release() noexcept;
The member function stores pointer()
in stored_ptr
and returns its previous contents.
unique_ptr::reset
void reset(pointer ptr = pointer()) noexcept; void reset(nullptr_t ptr) noexcept;
If ptr == stored_ptr
the member function does nothing.
Otherwise, if ptr != 0
it calls get_deleter()(stored_ptr)
and stores
ptr
in stored_ptr
.
unique_ptr::swap
void swap(unique_ptr& right) noexcept;
The member function swaps stored_ptr
with right.stored_ptr
and stored_deleter
with right.stored_deleter
.
unique_ptr::unique_ptr
constexpr unique_ptr() noexcept; constexpr unique_ptr(nullptr_t) noexcept; explicit unique_ptr(pointer ptr) noexcept; unique_ptr(Ty *ptr, typename conditional<is_reference<Del>::value, Del, typename add_reference<const Del>::type>::type deleter) noexcept; unique_ptr(pointer ptr, typename remove_reference<Del>::type&& deleter) noexcept; unique_ptr(unique_ptr&& right) noexcept; template<class Ty2, Class Del2> unique_ptr(unique_ptr<Ty2, Del2>&& right) noexcept; template<class Ty2> unique_ptr(auto_ptr<Ty2>&& right) noexcept;
The first two constructors construct an object that manages no resource.
The third constructor stores ptr
in stored_ptr
.
The fourth constructor stores ptr
in stored_ptr
and deleter
in stored_deleter
.
The fifth costructor stores ptr
in stored_ptr
and moves deleter
into stored_deleter
.
The sixth and seventh constructors store right.reset()
in stored_ptr
and forward right.get_deleter()
into stored_deleter
.
The last constructor stores right.reset()
in stored_ptr
.
Unless is_convertible<Other *, Ty *>
holds true,
and is_array<Ty2>
holds false,
and either is_reference<Del2>
holds true
and is_same<Del, Del2>
holds true
or is_reference<Del2>
holds false
and is_convertible<Del, Del2>
holds true,
the template constructor
unique_ptr(unique_ptr<Ty2, Del2>&& right)
does not participate in overload resolution.
Unless is_convertible<Other *, Ty *>
holds true
and is_same<Del, default_delete<Ty>>
holds true,
the template constructor
unique_ptr(auto_ptr<Ty2>&& right)
does not participate in overload resolution.
unique_ptr::~unique_ptr
~unique_ptr();
The destructor calls get_deleter()(stored_ptr)
.
unique_ptr<Ty[], Del>
template<class Ty, class Del> struct unique_ptr<Ty[], Del> { [added with C++11] typedef Ty element_type; typedef Del deleter_type; typedef T1 pointer; constexpr unique_ptr() noexcept; constexpr unique_ptr(nullptr_t) noexcept; explicit unique_ptr(pointer ptr) noexcept; unique_ptr(pointer ptr, typename conditional<is_reference<Del>::value, Del, typename add_reference<const Del>::type>::type deleter) noexcept; unique_ptr(pointer ptr, typename remove_reference<Del>::type&& deleter) noexcept; unique_ptr(unique_ptr&& right) noexcept; ~unique_ptr(); unique_ptr& operator=(unique_ptr&& right) noexcept; unique_ptr& operator=(nullptr_t) noexcept; void swap(unique_ptr& right) noexcept; pointer release() noexcept; void reset(pointer ptr = pointer()) noexcept; void reset(nullptr_t ptr) noexcept; pointer get() const noexcept; Ty& operator[](size_t idx) const noexcept; Del& get_deleter() noexcept; const Del& get_deleter() const noexcept; explicit operator bool() const noexcept; unique_ptr(const unique_ptr& right) = delete; unique_ptr& operator=(const unique_ptr& right) = delete; template<class Ptr2> void reset(_Ptr2 ptr) = delete; private: pointer stored_ptr; // exposition only Del stored_deleter; // exposition only };
The template class describes an object that uniquely manages an array resource.
Each unique_ptr
object effectively stores a pointer to the array
object that it owns or stores a null pointer. A resource can be owned by no
more than one unique_ptr
object; when the unique_ptr
object that owns a particular resource is destroyed the resource is freed.
A unique_ptr
object may be moved, but not copied.
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<Ty[]>
assumes that the resource pointed to by ptr
is allocated with
operator new[]
, so it can be freed by calling Del()(ptr)
.
(The general template unique_ptr<Ty>
manages non-array objects allocated with operator new
, and has the default deleter
default_delete<Ty>
.)
The stored pointer to a managed resource stored_ptr
has type
pointer
. It is Del::pointer
if defined, otherwise Ty *
. The stored deleter object
stored_deleter
should occupy no space in the object if the deleter is stateless.
Note that Del
can be a reference type.
unique_ptr::operator[]
Ty& operator[](size_t idx) const noexcept;
The member function returns stored_ptr[idx]
.
unique_ptr::unique_ptr
constexpr unique_ptr() noexcept; constexpr unique_ptr(nullptr_t) noexcept; explicit unique_ptr(pointer ptr) noexcept; unique_ptr(Ty *ptr, typename conditional<is_reference<Del>::value, Del, typename add_reference<const Del>::type>::type deleter) noexcept; unique_ptr(pointer ptr, typename remove_reference<Del>::type&& deleter) noexcept; unique_ptr(unique_ptr&& right) noexcept;
The first two constructors construct an object that manages no resource.
The third constructor stores ptr
in stored_ptr
.
The fourth constructor stores ptr
in stored_ptr
and deleter
in stored_deleter
.
The fifth costructor stores ptr
in stored_ptr
and moves deleter
into stored_deleter
.
The sixth constructor stores right.reset()
in stored_ptr
and moves right.get_deleter()
into stored_deleter
.
uses_allocator
template<class Ty, class Alloc> struct uses_allocator; [added with C++11]
The template struct
holds true only if
Ty::allocator_type
exists and
is_convertible<Alloc, Ty::allocator_type>
holds true.
The template may be specialized, for a library type or user-defined type, to hold true only if:
allocator_arg_t
and the second argument has type Alloc
, orAlloc
A type that meets these requirements is said to be allocator enabled.
weak_ptr
template<class Ty> class weak_ptr { [added with C++11] public: typedef Ty element_type; constexpr weak_ptr() noexcept; weak_ptr(const weak_ptr&) noexcept; template<class Other> weak_ptr(const weak_ptr<Other>&) noexcept; template<class Other> weak_ptr(const shared_ptr<Other>&) noexcept; weak_ptr& operator=(const weak_ptr&) noexcept; template<class Other> weak_ptr& operator=(const weak_ptr<Other>&) noexcept; template<class Other> weak_ptr& operator=(shared_ptr<Other>&) noexcept; void swap(weak_ptr& right) noexcept; void reset() noexcept; long use_count() const noexcept; bool expired() const noexcept; shared_ptr<Ty> lock() const noexcept; template<class Other> bool owner_before(const shared_ptr<Other>& ptr); [added with C++11] template<class Other> bool owner_before(const weak_ptr<Other>& ptr); [added with C++11] };
The template class describes an object that points to
a resource that is managed by one or more shared_ptr
objects. The weak_ptr
objects that point to a resource do not affect the
resource's reference count. Thus, when the last shared_ptr
object
that manages that resource is destroyed the resource will be freed, even if
there are weak_ptr
objects pointing to that resource. This is
essential for avoiding cycles in data structures.
A weak_ptr
object points to a resource
if it was constructed from a shared_ptr
object that owns
that resource, if it was constructed from a weak_ptr
object that
points to that resource, or if that resource was assigned to it with
operator=
. A weak_ptr
object does
not provide direct access to the resource that it points to. Code that needs to use
the resource does so through a shared_ptr
object that owns that resource,
created by calling the member function lock. A
weak_ptr
object has expired when the resource that
it points to has been freed because all of the shared_ptr
objects that own
the resource have been destroyed. Calling lock
on a weak_ptr
object
that has expired creates an empty shared_ptr object.
An empty weak_ptr object does not point to any
resources and has no control block. Its member function lock
returns
an empty shared_ptr object.
A cycle occurs when two or more resources controlled by
shared_ptr
objects hold mutually referencing shared_ptr
objects.
For example, a circular linked list with three elements has a head node N0
;
that node holds a shared_ptr
object that owns the next node, N1
; that
node holds a shared_ptr
object that owns the next node, N2
; that node,
in turn, holds a shared_ptr
object that owns the head node, N0
, closing
the cycle. In this situation, none of the reference counts will every become zero, and
the nodes in the cycle will not be freed. To eliminate the cycle, the last node
N2
should hold a weak_ptr
object pointing to N0
instead
of a smart_ptr
object. Since the weak_ptr
object does not own
N0
it doesn't affect N0
's reference count, and when the program's
last reference to the head node is destroyed the nodes in the list will also be destroyed.
weak_ptr::element_type
typedef Ty element_type;
The type is a synonym for the template parameter Ty
.
weak_ptr::expired
bool expired() const;
The member function returns true
if *this
has
expired, otherwise false
.
weak_ptr::lock
shared_ptr<Ty> lock() const;
The member function returns an empty shared_ptr
object if *this
has expired; otherwise it returns
a shared_ptr<Ty>
object that
owns
the resource that *this
points to.
weak_ptr::operator=
weak_ptr& operator=(const weak_ptr& wp); template<class Other> weak_ptr& operator=(const weak_ptr<Other>& wp); template<class Other> weak_ptr& operator=(const shared_ptr<Other>& sp);
The operators all release the resource currently
pointed to by *this
and assign ownership of the
resource named by the operand sequence to
*this
. If an operator fails it leaves
*this
unchanged.
weak_ptr::owner_before
template<class Other> bool owner_before(const shared_ptr<Other>& ptr); [added with C++11] template<class Other> bool owner_before(const weak_ptr<Other>& ptr); [added with C++11]
The template member function returns true if *this
is
ordered before ptr
.
weak_ptr::reset
void reset() noexcept;
The member function releases the
resource pointed to by *this
and converts *this
to an
empty weak_ptr object.
weak_ptr::swap
void swap(weak_ptr& right) noexcept;
The member function leaves the resource originally pointed to
by *this
subsequently pointed to by wp
, and the resource
originally pointed to by wp
subsequently pointed to by *this
.
The function does not change the reference counts for the two resources and
it does not throw any exceptions.
weak_ptr::use_count
long use_count() const;
The member function returns the number of shared_ptr
objects
that own the resource
pointed to by *this
.
weak_ptr::weak_ptr
constexpr weak_ptr(); weak_ptr(const weak_ptr& wp); template<class Other> weak_ptr(const weak_ptr<Other>& wp); template<class Other> weak_ptr(const shared_ptr<Other>& sp);
The constructors each construct an object that points to the resource named by the operand sequence.
Unless is_convertible<Other *, Ty *>
holds true,
the template constructor
weak_ptr(const weak_ptr<Other>& wp)
does not participate in overload resolution.
Unless is_convertible<Other *, Ty *>
holds true,
the template constructor
weak_ptr(const shared_ptr<Other>& wp)
does not participate in overload resolution.
See also the Table of Contents and the Index.
Copyright © 1992-2013 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.