A weak reference to a strong_ptr. More...
#include <memory.hpp>
Public Types | |
| using | element_type = T |
Public Member Functions | |
| weak_ptr () noexcept=default | |
| Default constructor creates empty weak_ptr. | |
| weak_ptr (strong_ptr< T > const &p_strong) noexcept | |
| Create weak_ptr from strong_ptr. | |
| weak_ptr (weak_ptr const &p_other) noexcept | |
| Copy constructor. | |
| weak_ptr (weak_ptr &&p_other) noexcept | |
| Move constructor. | |
| template<typename U > requires (std::is_convertible_v<U*, T*>) | |
| weak_ptr (weak_ptr< U > const &p_other) noexcept | |
| Converting copy constructor. | |
| template<typename U > requires (std::is_convertible_v<U*, T*>) | |
| weak_ptr (weak_ptr< U > &&p_other) noexcept | |
| Converting move constructor. | |
| template<typename U > requires (std::is_convertible_v<U*, T*>) | |
| weak_ptr (strong_ptr< U > const &p_other) noexcept | |
| Converting constructor from strong_ptr. | |
| ~weak_ptr () | |
| Destructor. | |
| weak_ptr & | operator= (weak_ptr const &p_other) noexcept |
| Copy assignment operator. | |
| weak_ptr & | operator= (weak_ptr &&p_other) noexcept |
| Move assignment operator. | |
| weak_ptr & | operator= (strong_ptr< T > const &p_strong) noexcept |
| Assignment from strong_ptr. | |
| void | swap (weak_ptr &p_other) noexcept |
| Swap the contents of this weak_ptr with another. | |
| bool | expired () const noexcept |
| Check if the referenced object has been destroyed. | |
| optional_ptr< T > | lock () const noexcept |
| Attempt to obtain a strong_ptr to the referenced object. | |
| auto | use_count () const noexcept |
| Get the current strong reference count. | |
Friends | |
| template<typename U > | |
| class | strong_ptr |
A weak reference to a strong_ptr.
weak_ptr provides a non-owning reference to an object managed by strong_ptr. It can be used to break reference cycles or to create an optional_ptr.
A weak_ptr doesn't increase the strong reference count, so it doesn't prevent the object from being destroyed when the last strong_ptr goes away.
Example usage:
| T | The type of the referenced object |
|
inlinenoexcept |
Create weak_ptr from strong_ptr.
| p_strong | The strong_ptr to create a weak reference to |
|
inlinenoexcept |
Copy constructor.
| p_other | The weak_ptr to copy from |
|
inlinenoexcept |
Move constructor.
| p_other | The weak_ptr to move from |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Converting constructor from strong_ptr.
Creates a weak_ptr<T> from a strong_ptr where U is convertible to T.
| U | A type convertible to T |
| p_other | The strong_ptr to create a weak reference to |
|
inline |
Destructor.
Decrements the weak reference count and potentially deallocates memory if this was the last reference.
|
inlinenoexcept |
Check if the referenced object has been destroyed.
|
inlinenoexcept |
Attempt to obtain a strong_ptr to the referenced object.
Implement weak_ptr::lock() now that optional_ptr is defined.
If the object still exists, returns an optional_ptr containing a strong_ptr to it. Otherwise, returns an empty optional_ptr.
This function attempts to obtain a strong_ptr from a weak_ptr. If the referenced object still exists, it returns an optional_ptr containing a strong_ptr to it. Otherwise, it returns an empty optional_ptr.
| T | The type of the referenced object |
|
inlinenoexcept |
Assignment from strong_ptr.
| p_strong | The strong_ptr to create a weak reference to |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Get the current strong reference count.
This is primarily for testing purposes.