template <typename T>
rll::observer_ptr class

A smart pointer that holds a pointer to a value, but does not own it.

Template parameters
T Type of the pointer

observer_ptr is a non-owning pointer, or observer. The observer stores a pointer to a second object, known as the watched object. An observer_ptr may also have no watched object.

An observer is not responsible in any way for the watched object; there is no inherent relationship between an observer and the object it watches.

It is intended as a near drop-in replacement for raw pointer types, with the advantage that, as a vocabulary type, it indicates its intended use without need for detailed analysis by code readers.

Public types

using const_pointer = T const *
The const pointer type.
using const_reference = T const &
The const reference type.
using element_type = T
The element type.
using pointer = T*
The pointer type.
using reference = T&
The reference type.
using value_type = std::remove_cv_t<T>
The value type. Equivalent to the element type without cv-qualifiers.

Constructors, destructors, conversion operators

observer_ptr() constexpr noexcept
Constructs nullptr observer.
observer_ptr(std::nullptr_t) constexpr noexcept
Constructs nullptr observer.
observer_ptr(pointer ptr) constexpr noexcept
Constructs an observer from a pointer.
template <typename U, typename = std::enable_if_t<std::is_convertible<U*, T*>::value>>
observer_ptr(observer_ptr<U> const& other) constexpr noexcept
Constructs an observer from another observer.
template <typename U, typename = std::enable_if_t<std::is_convertible<U*, T*>::value>>
observer_ptr(std::unique_ptr<U> const& other) constexpr noexcept
Constructs an observer from a unique pointer.
operator bool() const explicit noexcept
Returns true if the watched pointer is not nullptr.
operator pointer() const explicit noexcept
Returns the watched pointer.

Public functions

auto get() const →  pointer constexpr noexcept
Returns the watched pointer.
auto operator*() const →  reference constexpr noexcept
Returns the dereferenced watched object.
auto operator->() const →  pointer constexpr noexcept
Returns the pointer to the watched object.
auto ref() const →  const_reference constexpr noexcept
Returns the constant reference to the watched object.
auto ref() →  reference constexpr noexcept
Returns the reference to the watched object.
auto release() →  pointer constexpr noexcept
Returns the watched pointer and resets the watched pointer to nullptr.
void reset(pointer ptr = nullptr) constexpr noexcept
Resets the watched pointer to nullptr or the new watched pointer.
void swap(observer_ptr& other) constexpr noexcept
Swaps two watched pointers.

Function documentation

template <typename T>
rll::observer_ptr::observer_ptr(pointer ptr) constexpr noexcept

Constructs an observer from a pointer.

Parameters
ptr Pointer to construct from.

template <typename T>
template <typename U, typename = std::enable_if_t<std::is_convertible<U*, T*>::value>>
rll::observer_ptr::observer_ptr(observer_ptr<U> const& other) constexpr noexcept

Constructs an observer from another observer.

Template parameters
U Type of the other observer. Must be convertible to T*.
Parameters
other Observer to construct from.

template <typename T>
template <typename U, typename = std::enable_if_t<std::is_convertible<U*, T*>::value>>
rll::observer_ptr::observer_ptr(std::unique_ptr<U> const& other) constexpr noexcept

Constructs an observer from a unique pointer.

Template parameters
U Underlying type of the unique pointer. Must be convertible to T*.
Parameters
other Unique pointer to construct from.

template <typename T>
rll::observer_ptr::operator pointer() const explicit noexcept

Returns the watched pointer.

Returns The watched pointer.

template <typename T>
pointer rll::observer_ptr::get() const constexpr noexcept

Returns the watched pointer.

Returns The watched pointer.

template <typename T>
reference rll::observer_ptr::operator*() const constexpr noexcept

Returns the dereferenced watched object.

Returns The reference to the watched object.

template <typename T>
pointer rll::observer_ptr::operator->() const constexpr noexcept

Returns the pointer to the watched object.

Returns The pointer to the watched object.

template <typename T>
const_reference rll::observer_ptr::ref() const constexpr noexcept

Returns the constant reference to the watched object.

Returns The reference to the watched object.

template <typename T>
reference rll::observer_ptr::ref() constexpr noexcept

Returns the reference to the watched object.

Returns The reference to the watched object.

template <typename T>
pointer rll::observer_ptr::release() constexpr noexcept

Returns the watched pointer and resets the watched pointer to nullptr.

Returns The watched pointer.

template <typename T>
void rll::observer_ptr::reset(pointer ptr = nullptr) constexpr noexcept

Resets the watched pointer to nullptr or the new watched pointer.

Parameters
ptr The new watched pointer. Defaults to nullptr.

template <typename T>
void rll::observer_ptr::swap(observer_ptr& other) constexpr noexcept

Swaps two watched pointers.

Parameters
other The other observer.