throwing_ptr
Smart pointers that throw on dereference if null
Public Types | Public Member Functions | Friends | List of all members
throwing::shared_ptr< T > Class Template Reference

Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced. More...

Public Types

typedef std::shared_ptr< T >::element_type element_type
 the type pointed to. More...
 

Public Member Functions

TSP_CONSTEXPR shared_ptr () TSP_NOEXCEPT=default
 Constructs a shared_ptr with no managed object, i.e. empty shared_ptr. More...
 
TSP_CONSTEXPR shared_ptr (std::nullptr_t ptr) TSP_NOEXCEPT
 Constructs a shared_ptr with no managed object, i.e. empty shared_ptr. More...
 
template<class Y >
 shared_ptr (Y *ptr)
 Constructs a shared_ptr with ptr as the pointer to the managed object. More...
 
template<class Y , class Deleter >
 shared_ptr (Y *ptr, Deleter d)
 Constructs a shared_ptr with ptr as the pointer to the managed object. More...
 
template<class Deleter >
 shared_ptr (std::nullptr_t ptr, Deleter d)
 Constructs a shared_ptr with ptr as the pointer to the managed object. More...
 
template<class Y , class Deleter , class Alloc >
 shared_ptr (Y *ptr, Deleter d, Alloc alloc)
 Constructs a shared_ptr with ptr as the pointer to the managed object. More...
 
template<class Y >
 shared_ptr (const shared_ptr< Y > &r, element_type *ptr) TSP_NOEXCEPT
 The aliasing constructor. More...
 
 shared_ptr (const shared_ptr &r) TSP_NOEXCEPT
 Constructs a shared_ptr which shares ownership of the object managed by r. More...
 
template<class Y >
 shared_ptr (const shared_ptr< Y > &r) TSP_NOEXCEPT
 Constructs a shared_ptr which shares ownership of the object managed by r. More...
 
 shared_ptr (shared_ptr &&r) TSP_NOEXCEPT
 Move-constructs a shared_ptr from r. More...
 
template<class Y >
 shared_ptr (shared_ptr< Y > &&r) TSP_NOEXCEPT
 Move-constructs a shared_ptr from r. More...
 
 shared_ptr (const std::shared_ptr< T > &r) TSP_NOEXCEPT
 Constructs a shared_ptr which shares ownership of the object managed by r. More...
 
template<class Y >
 shared_ptr (const std::shared_ptr< Y > &r) TSP_NOEXCEPT
 Constructs a shared_ptr which shares ownership of the object managed by r. More...
 
 shared_ptr (std::shared_ptr< T > &&r) TSP_NOEXCEPT
 Move-constructs a shared_ptr from r. More...
 
template<class Y >
 shared_ptr (std::shared_ptr< Y > &&r) TSP_NOEXCEPT
 Move-constructs a shared_ptr from r. More...
 
template<class Y >
 shared_ptr (const std::weak_ptr< Y > &r)
 Constructs a shared_ptr which shares ownership of the object managed by r. More...
 
template<class Y , class Deleter >
 shared_ptr (std::unique_ptr< Y, Deleter > &&r)
 Constructs a shared_ptr which manages the object currently managed by r. More...
 
 ~shared_ptr ()=default
 Destructor If *this owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. After the destruction, the smart pointers that shared ownership with *this, if any, will report a use_count() that is one less than its previous value. More...
 
shared_ptroperator= (const shared_ptr &r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class Y >
shared_ptroperator= (const shared_ptr< Y > &r) TSP_NOEXCEPT
 Assignment operator. More...
 
shared_ptroperator= (shared_ptr &&r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class Y >
shared_ptroperator= (shared_ptr< Y > &&r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class Y , class Deleter >
shared_ptroperator= (std::unique_ptr< Y, Deleter > &&r)
 Assignment operator. More...
 
void swap (shared_ptr &r) TSP_NOEXCEPT
 Exchanges the contents of *this and r. More...
 
void reset () TSP_NOEXCEPT
 Releases the ownership of the managed object, if any. More...
 
template<class Y >
void reset (Y *ptr)
 Replaces the managed object with an object pointed to by ptr. More...
 
template<class Y , class Deleter >
void reset (Y *ptr, Deleter d)
 Replaces the managed object with an object pointed to by ptr. More...
 
template<class Y , class Deleter , class Alloc >
void reset (Y *ptr, Deleter d, Alloc alloc)
 Replaces the managed object with an object pointed to by ptr. More...
 
element_typeget () const TSP_NOEXCEPT
 Returns the stored pointer. More...
 
const std::shared_ptr< T > & get_std_shared_ptr () const TSP_NOEXCEPT
 Returns the underlying std::shared_pointer. More...
 
std::shared_ptr< T > & get_std_shared_ptr () TSP_NOEXCEPT
 Returns the underlying std::shared_pointer. More...
 
T & operator* () const
 Dereferences the stored pointer. More...
 
T * operator-> () const
 Dereferences the stored pointer. More...
 
long use_count () const TSP_NOEXCEPT
 Returns the number of different shared_ptr instances (this included) managing the current object. More...
 
 operator bool () const TSP_NOEXCEPT
 Checks if *this stores a non-null pointer, i.e. whether get() != nullptr. More...
 
template<class Y >
bool owner_before (const shared_ptr< Y > &other) const TSP_NOEXCEPT
 Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. More...
 
template<class Y >
bool owner_before (const std::shared_ptr< Y > &other) const TSP_NOEXCEPT
 Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. More...
 
template<class Y >
bool owner_before (const std::weak_ptr< Y > &other) const TSP_NOEXCEPT
 Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. More...
 

Friends

template<typename Y >
class shared_ptr
 

Detailed Description

template<typename T>
class throwing::shared_ptr< T >

Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced.

throwing::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens:

The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.

A shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used to point to member objects while owning the object they belong to. The stored pointer is the one accessed by get(), the dereference and the comparison operators. The managed pointer is the one passed to the deleter when use count reaches zero.

A shared_ptr may also own no objects, in which case it is called empty (an empty shared_ptr may have a non-null stored pointer if the aliasing constructor was used to create it).

All specializations of shared_ptr meet the requirements of CopyConstructible, CopyAssignable, and LessThanComparable and are contextually convertible to bool.

All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the shared_ptr overloads of atomic functions can be used to prevent the data race.

The underlying std::shared_ptr is available via get_std_shared_ptr()

Definition at line 63 of file shared_ptr.hpp.

Member Typedef Documentation

◆ element_type

template<typename T>
typedef std::shared_ptr<T>::element_type throwing::shared_ptr< T >::element_type

the type pointed to.

Definition at line 66 of file shared_ptr.hpp.

Constructor & Destructor Documentation

◆ shared_ptr() [1/17]

template<typename T>
TSP_CONSTEXPR throwing::shared_ptr< T >::shared_ptr ( )
default

Constructs a shared_ptr with no managed object, i.e. empty shared_ptr.

◆ shared_ptr() [2/17]

template<typename T>
TSP_CONSTEXPR throwing::shared_ptr< T >::shared_ptr ( std::nullptr_t  ptr)
inline

Constructs a shared_ptr with no managed object, i.e. empty shared_ptr.

Definition at line 79 of file shared_ptr.hpp.

79 : p(ptr) {}

◆ shared_ptr() [3/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( Y *  ptr)
inlineexplicit

Constructs a shared_ptr with ptr as the pointer to the managed object.

Definition at line 84 of file shared_ptr.hpp.

84 : p(ptr) {}

◆ shared_ptr() [4/17]

template<typename T>
template<class Y , class Deleter >
throwing::shared_ptr< T >::shared_ptr ( Y *  ptr,
Deleter  d 
)
inline

Constructs a shared_ptr with ptr as the pointer to the managed object.

Uses the specified deleter d as the deleter.

The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions.

The construction of d and of the stored deleter from d must not throw exceptions.

Definition at line 98 of file shared_ptr.hpp.

98 : p(ptr, d) {}

◆ shared_ptr() [5/17]

template<typename T>
template<class Deleter >
throwing::shared_ptr< T >::shared_ptr ( std::nullptr_t  ptr,
Deleter  d 
)
inline

Constructs a shared_ptr with ptr as the pointer to the managed object.

Uses the specified deleter d as the deleter.

The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions.

The construction of d and of the stored deleter from d must not throw exceptions.

Definition at line 112 of file shared_ptr.hpp.

112 : p(ptr, d) {}

◆ shared_ptr() [6/17]

template<typename T>
template<class Y , class Deleter , class Alloc >
throwing::shared_ptr< T >::shared_ptr ( Y *  ptr,
Deleter  d,
Alloc  alloc 
)
inline

Constructs a shared_ptr with ptr as the pointer to the managed object.

Uses the specified deleter d as the deleter.

The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions.

The construction of d and of the stored deleter from d must not throw exceptions.

Uses a copy of alloc for allocation of data for internal use.

Alloc must be a Allocator.

Definition at line 130 of file shared_ptr.hpp.

130 : p(ptr, d, alloc) {}

◆ shared_ptr() [7/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( const shared_ptr< Y > &  r,
element_type ptr 
)
inline

The aliasing constructor.

Constructs a shared_ptr which shares ownership information with r, but holds an unrelated and unmanaged pointer ptr. Even if this shared_ptr is the last of the group to go out of scope, it will call the destructor for the object originally managed by r.

However, calling get() on this will always return a copy of ptr.

It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by r or is an alias (e.g., downcast) of r.get()

Definition at line 147 of file shared_ptr.hpp.

148  : p(r.p, ptr) {}

◆ shared_ptr() [8/17]

template<typename T>
throwing::shared_ptr< T >::shared_ptr ( const shared_ptr< T > &  r)
inline

Constructs a shared_ptr which shares ownership of the object managed by r.

If r manages no object, *this manages no object too.

Definition at line 155 of file shared_ptr.hpp.

155 : p(r.p) {}

◆ shared_ptr() [9/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( const shared_ptr< Y > &  r)
inline

Constructs a shared_ptr which shares ownership of the object managed by r.

If r manages no object, *this manages no object too.

Definition at line 163 of file shared_ptr.hpp.

163 : p(r.p) {}

◆ shared_ptr() [10/17]

template<typename T>
throwing::shared_ptr< T >::shared_ptr ( shared_ptr< T > &&  r)
inline

Move-constructs a shared_ptr from r.

After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.

Definition at line 170 of file shared_ptr.hpp.

170 : p(std::move(r.p)) {}

◆ shared_ptr() [11/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( shared_ptr< Y > &&  r)
inline

Move-constructs a shared_ptr from r.

After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.

Definition at line 178 of file shared_ptr.hpp.

178 : p(std::move(r.p)) {}

◆ shared_ptr() [12/17]

template<typename T>
throwing::shared_ptr< T >::shared_ptr ( const std::shared_ptr< T > &  r)
inline

Constructs a shared_ptr which shares ownership of the object managed by r.

If r manages no object, *this manages no object too.

Definition at line 185 of file shared_ptr.hpp.

185 : p(r) {}

◆ shared_ptr() [13/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( const std::shared_ptr< Y > &  r)
inline

Constructs a shared_ptr which shares ownership of the object managed by r.

If r manages no object, *this manages no object too.

Definition at line 193 of file shared_ptr.hpp.

193 : p(r) {}

◆ shared_ptr() [14/17]

template<typename T>
throwing::shared_ptr< T >::shared_ptr ( std::shared_ptr< T > &&  r)
inline

Move-constructs a shared_ptr from r.

After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.

Definition at line 200 of file shared_ptr.hpp.

200 : p(std::move(r)) {}

◆ shared_ptr() [15/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( std::shared_ptr< Y > &&  r)
inline

Move-constructs a shared_ptr from r.

After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.

Definition at line 208 of file shared_ptr.hpp.

208 : p(std::move(r)) {}

◆ shared_ptr() [16/17]

template<typename T>
template<class Y >
throwing::shared_ptr< T >::shared_ptr ( const std::weak_ptr< Y > &  r)
inlineexplicit

Constructs a shared_ptr which shares ownership of the object managed by r.

Y* must be implicitly convertible to T*. (until C++17)

This overload only participates in overload resolution if Y* is compatible with T*. (since C++17)

Note that r.lock() may be used for the same purpose: the difference is that this constructor throws an exception if the argument is empty, while std::weak_ptr<T>::lock() constructs an empty std::shared_ptr in that case.

Definition at line 223 of file shared_ptr.hpp.

223 : p(r) {}

◆ shared_ptr() [17/17]

template<typename T>
template<class Y , class Deleter >
throwing::shared_ptr< T >::shared_ptr ( std::unique_ptr< Y, Deleter > &&  r)
inline

Constructs a shared_ptr which manages the object currently managed by r.

The deleter associated with r is stored for future deletion of the managed object.

r manages no object after the call.

This overload doesn't participate in overload resolution if std::unique_ptr<Y, Deleter>::pointer is not compatible with T*. If r.get() is a null pointer, this overload is equivalent to the default constructor (1). (since C++17)

If Deleter is a reference type, equivalent to shared_ptr(r.release(), std::ref(r.get_deleter()). Otherwise, equivalent to shared_ptr(r.release(), r.get_deleter())

Definition at line 243 of file shared_ptr.hpp.

243 : p(std::move(r)) {}

◆ ~shared_ptr()

template<typename T>
throwing::shared_ptr< T >::~shared_ptr ( )
default

Destructor If *this owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. After the destruction, the smart pointers that shared ownership with *this, if any, will report a use_count() that is one less than its previous value.

Notes:

Unlike throwing::unique_ptr, the throwing of std::shared_ptr is invoked even if the managed pointer is null.

Member Function Documentation

◆ get()

template<typename T>
element_type* throwing::shared_ptr< T >::get ( ) const
inline

Returns the stored pointer.

Definition at line 407 of file shared_ptr.hpp.

407 { return p.get(); }

◆ get_std_shared_ptr() [1/2]

template<typename T>
const std::shared_ptr<T>& throwing::shared_ptr< T >::get_std_shared_ptr ( ) const
inline

Returns the underlying std::shared_pointer.

Definition at line 411 of file shared_ptr.hpp.

411  {
412  return p;
413  }

◆ get_std_shared_ptr() [2/2]

template<typename T>
std::shared_ptr<T>& throwing::shared_ptr< T >::get_std_shared_ptr ( )
inline

Returns the underlying std::shared_pointer.

Definition at line 417 of file shared_ptr.hpp.

417 { return p; }

◆ operator bool()

template<typename T>
throwing::shared_ptr< T >::operator bool ( ) const
inlineexplicit

Checks if *this stores a non-null pointer, i.e. whether get() != nullptr.

Definition at line 475 of file shared_ptr.hpp.

475 { return p.operator bool(); }

◆ operator*()

template<typename T>
T& throwing::shared_ptr< T >::operator* ( ) const
inline

Dereferences the stored pointer.

Exceptions
null_ptr_exception<T>if the pointer is null

Definition at line 423 of file shared_ptr.hpp.

423  {
424  const auto ptr = get();
425  if (nullptr == ptr)
426  throw null_ptr_exception<T>();
427  return *ptr;
428  }

◆ operator->()

template<typename T>
T* throwing::shared_ptr< T >::operator-> ( ) const
inline

Dereferences the stored pointer.

Exceptions
null_ptr_exception<T>if the pointer is null

Definition at line 434 of file shared_ptr.hpp.

434  {
435  const auto ptr = get();
436  if (nullptr == ptr)
437  throw null_ptr_exception<T>();
438  return ptr;
439  }

◆ operator=() [1/5]

template<typename T>
shared_ptr& throwing::shared_ptr< T >::operator= ( const shared_ptr< T > &  r)
inline

Assignment operator.

Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.

Shares ownership of the object managed by r. If r manages no object, *this manages no object too. Equivalent to shared_ptr<T>(r).swap(*this).

Definition at line 270 of file shared_ptr.hpp.

270  {
271  p = r.p;
272  return *this;
273  }

◆ operator=() [2/5]

template<typename T>
template<class Y >
shared_ptr& throwing::shared_ptr< T >::operator= ( const shared_ptr< Y > &  r)
inline

Assignment operator.

Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.

Shares ownership of the object managed by r. If r manages no object, *this manages no object too. Equivalent to shared_ptr<T>(r).swap(*this).

Definition at line 287 of file shared_ptr.hpp.

287  {
288  p = r.p;
289  return *this;
290  }

◆ operator=() [3/5]

template<typename T>
shared_ptr& throwing::shared_ptr< T >::operator= ( shared_ptr< T > &&  r)
inline

Assignment operator.

Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.

Move-assigns a shared_ptr from r. After the assignment, *this contains a copy of the previous state of r, r is empty. Equivalent to shared_ptr<T>(std::move(r)).swap(*this)

Definition at line 303 of file shared_ptr.hpp.

303  {
304  p = std::move(r.p);
305  return *this;
306  }

◆ operator=() [4/5]

template<typename T>
template<class Y >
shared_ptr& throwing::shared_ptr< T >::operator= ( shared_ptr< Y > &&  r)
inline

Assignment operator.

Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.

Move-assigns a shared_ptr from r. After the assignment, *this contains a copy of the previous state of r, r is empty. Equivalent to shared_ptr<T>(std::move(r)).swap(*this)

Definition at line 319 of file shared_ptr.hpp.

319  {
320  p = std::move(r.p);
321  return *this;
322  }

◆ operator=() [5/5]

template<typename T>
template<class Y , class Deleter >
shared_ptr& throwing::shared_ptr< T >::operator= ( std::unique_ptr< Y, Deleter > &&  r)
inline

Assignment operator.

Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.

Transfers the ownership of the object managed by r to *this. The deleter associated to r is stored for future deletion of the managed object. r manages no object after the call. Equivalent to shared_ptr<T>(std::move(r)).swap(*this).

Definition at line 337 of file shared_ptr.hpp.

337  {
338  p = std::move(r);
339  return *this;
340  }

◆ owner_before() [1/3]

template<typename T>
template<class Y >
bool throwing::shared_ptr< T >::owner_before ( const shared_ptr< Y > &  other) const
inline

Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order.

The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)

This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.

Definition at line 489 of file shared_ptr.hpp.

489  {
490  return p.owner_before(other.p);
491  }

◆ owner_before() [2/3]

template<typename T>
template<class Y >
bool throwing::shared_ptr< T >::owner_before ( const std::shared_ptr< Y > &  other) const
inline

Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order.

The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)

This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.

Definition at line 505 of file shared_ptr.hpp.

505  {
506  return p.owner_before(other);
507  }

◆ owner_before() [3/3]

template<typename T>
template<class Y >
bool throwing::shared_ptr< T >::owner_before ( const std::weak_ptr< Y > &  other) const
inline

Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order.

The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)

This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.

Definition at line 521 of file shared_ptr.hpp.

521  {
522  return p.owner_before(other);
523  }

◆ reset() [1/4]

template<typename T>
void throwing::shared_ptr< T >::reset ( )
inline

Releases the ownership of the managed object, if any.

If *this already owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. If the object pointed to by ptr is already owned, the function results in undefined behavior.

After the call, *this manages no object. Equivalent to shared_ptr().swap(*this);

Definition at line 356 of file shared_ptr.hpp.

356 { p.reset(); }

◆ reset() [2/4]

template<typename T>
template<class Y >
void throwing::shared_ptr< T >::reset ( Y *  ptr)
inline

Replaces the managed object with an object pointed to by ptr.

Y must be a complete type and implicitly convertible to T.

Uses the delete expression as the deleter. A valid delete expression must be available, i.e. delete ptr must be well formed, have well-defined behavior and not throw any exceptions.

Equivalent to shared_ptr<T>(ptr).swap(*this);

Definition at line 368 of file shared_ptr.hpp.

368 { p.reset(ptr); }

◆ reset() [3/4]

template<typename T>
template<class Y , class Deleter >
void throwing::shared_ptr< T >::reset ( Y *  ptr,
Deleter  d 
)
inline

Replaces the managed object with an object pointed to by ptr.

Y must be a complete type and implicitly convertible to T.

Uses the specified deleter d as the deleter. Deleter must be callable for the type T, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter must be CopyConstructible, and its copy constructor and destructor must not throw exceptions.

Equivalent to shared_ptr<T>(ptr, d).swap(*this);

Definition at line 381 of file shared_ptr.hpp.

381  {
382  p.reset(ptr, d);
383  }

◆ reset() [4/4]

template<typename T>
template<class Y , class Deleter , class Alloc >
void throwing::shared_ptr< T >::reset ( Y *  ptr,
Deleter  d,
Alloc  alloc 
)
inline

Replaces the managed object with an object pointed to by ptr.

Y must be a complete type and implicitly convertible to T.

Uses the specified deleter d as the deleter. Deleter must be callable for the type T, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter must be CopyConstructible, and its copy constructor and destructor must not throw exceptions.

Additionally uses a copy of alloc for allocation of data for internal use. Alloc must be a Allocator. The copy constructor and destructor must not throw exceptions.

Equivalent to shared_ptr<T>(ptr, d, alloc).swap(*this);

Definition at line 401 of file shared_ptr.hpp.

401  {
402  p.reset(ptr, d, alloc);
403  }

◆ swap()

template<typename T>
void throwing::shared_ptr< T >::swap ( shared_ptr< T > &  r)
inline

Exchanges the contents of *this and r.

Definition at line 344 of file shared_ptr.hpp.

344 { p.swap(r.p); }

◆ use_count()

template<typename T>
long throwing::shared_ptr< T >::use_count ( ) const
inline

Returns the number of different shared_ptr instances (this included) managing the current object.

If there is no managed object, ​0​ is returned.

In multithreaded environment, the value returned by use_count is approximate (typical implementations use a memory_order_relaxed load)

Definition at line 470 of file shared_ptr.hpp.

470 { return p.use_count(); }

Friends And Related Function Documentation

◆ shared_ptr

template<typename T>
template<typename Y >
friend class shared_ptr
friend

Definition at line 69 of file shared_ptr.hpp.


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