intrusive_ptr

A lightweight, header-only intrusive smart pointer for C++20.

namespace crimson_cell

Functions

template<class T>
constexpr void swap(intrusive_ptr<T> &lhs, intrusive_ptr<T> &rhs) noexcept

Exchanges the managed objects of two intrusive pointers.

Template Parameters:

T – The pointed-to type.

Parameters:
  • lhs – First intrusive pointer.

  • rhs – Second intrusive pointer.

template<class T>
constexpr bool operator==(const intrusive_ptr<T> &lhs, const std::nullptr_t rhs) noexcept

Compares an intrusive pointer with nullptr.

template<class T, class U>
constexpr bool operator==(const intrusive_ptr<T> &lhs, const intrusive_ptr<U> &rhs) noexcept

Compares two intrusive pointers.

template<class T, class U>
constexpr std::strong_ordering operator<=>(const intrusive_ptr<T> &lhs, const intrusive_ptr<U> &rhs) noexcept

Performs a three-way comparison of two intrusive pointers.

template<class T>
constexpr std::strong_ordering operator<=>(const intrusive_ptr<T> &lhs, std::nullptr_t) noexcept

Performs a three-way comparison of an intrusive pointer and null.

template<class T, class U>
intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const &p)

Performs a static cast between intrusive pointer types.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast.

Returns:

Converted intrusive pointer.

template<class T, class U>
intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> &&p) noexcept

Performs a static cast between intrusive pointer types.

Transfers ownership from the source pointer without modifying the reference count.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast. The pointer is emptied after the cast.

Returns:

Converted intrusive pointer owning the same object.

template<class T, class U>
intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const &p)

Performs a const cast between intrusive pointer types.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast.

Returns:

Converted intrusive pointer.

template<class T, class U>
intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> &&p) noexcept

Performs a const cast between intrusive pointer types.

Transfers ownership from the source pointer without modifying the reference count.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast. The pointer is empty after the cast.

Returns:

Converted intrusive pointer owning the same object.

template<class T, class U>
intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const &p)

Performs a dynamic cast between intrusive pointer types.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast.

Returns:

Converted intrusive pointer.

template<class T, class U>
intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> &&p) noexcept

Performs a dynamic cast between intrusive pointer types.

Transfers ownership from the source pointer without modifying the reference count if the cast succeeds.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast. The pointer is empty after a successful cast.

Returns:

Converted intrusive pointer owning the same object, or an empty pointer if the cast fails.

template<class T, class U>
intrusive_ptr<T> reinterpret_pointer_cast(const intrusive_ptr<U> &p)

Performs a reinterpret cast between intrusive pointer types.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast.

Returns:

Converted intrusive pointer.

template<class T, class U>
intrusive_ptr<T> reinterpret_pointer_cast(intrusive_ptr<U> &&p) noexcept

Performs a reinterpret cast between intrusive pointer types.

Transfers ownership from the source pointer without modifying the reference count.

Template Parameters:
  • T – Target type.

  • U – Source type.

Parameters:

p – Pointer to cast. The pointer is empty after the cast.

Returns:

Converted intrusive pointer owning the same object.

Variables

constexpr adopt_ref_t adopt_ref = {}

Tag used to adopt an existing reference without incrementing the reference count.

struct adopt_ref_t

Tag type used to adopt an existing reference without incrementing the reference count.

template<class T>
class intrusive_ptr

A lightweight intrusive reference-counted smart pointer.

The managed type is responsible for maintaining its own reference count.

The following functions must be available through ADL:

Template Parameters:

T – The managed object type.

Public Types

using element_type = T

The managed object type.

Public Functions

constexpr intrusive_ptr() noexcept = default

Constructs an empty intrusive pointer.

explicit intrusive_ptr(T *p)

Constructs a pointer from a raw pointer.

Parameters:

p – Pointer to the managed object.

explicit intrusive_ptr(T *p, adopt_ref_t tag)

Constructs a pointer by adopting an existing reference.

Does not increment the reference count. The caller must already own a reference to the object.

Parameters:
  • p – Pointer to the managed object.

  • tag – Tag indicating that the existing reference is adopted.

intrusive_ptr(const intrusive_ptr &other)

Copies an intrusive pointer and acquires a reference.

Parameters:

other – Pointer to copy.

intrusive_ptr(intrusive_ptr &&other) noexcept

Moves an intrusive pointer.

Parameters:

other – Pointer to move from.

template<class U>
intrusive_ptr(const intrusive_ptr<U> &other)

Converts an intrusive pointer to a compatible type.

Template Parameters:

U – Source object type.

Parameters:

other – Pointer to convert.

template<class U>
intrusive_ptr(intrusive_ptr<U> &&other) noexcept

Moves an intrusive pointer to a compatible type.

Template Parameters:

U – Source object type.

Parameters:

other – Pointer to move from.

~intrusive_ptr()

Releases the owned reference.

intrusive_ptr &operator=(const intrusive_ptr &other)

Copies another pointer.

Parameters:

other – Pointer to copy.

Returns:

Reference to this pointer.

intrusive_ptr &operator=(intrusive_ptr &&other) noexcept

Moves another pointer.

Parameters:

other – Pointer to move from.

Returns:

Reference to this pointer.

template<class U>
intrusive_ptr &operator=(const intrusive_ptr<U> &other)

Copies a compatible pointer type.

Template Parameters:

U – Source object type.

Parameters:

other – Pointer to copy.

Returns:

Reference to this pointer.

template<class U>
intrusive_ptr &operator=(intrusive_ptr<U> &&other) noexcept

Moves a compatible pointer type.

Template Parameters:

U – Source object type.

Parameters:

other – Pointer to move from.

Returns:

Reference to this pointer.

void reset()

Releases the current reference and becomes empty.

void reset(T *p)

Replaces the managed pointer.

Parameters:

p – Pointer to the new managed object.

constexpr T *get() const noexcept

Returns the stored pointer.

T *release() noexcept

Releases ownership without decrementing the reference count.

Returns:

The previously stored pointer.

constexpr T &operator*() const noexcept

Dereferences the managed object.

constexpr T *operator->() const noexcept

Accesses the managed object.

explicit operator bool() const noexcept

Checks whether the pointer contains an object.

void swap(intrusive_ptr &other) noexcept

Swaps two intrusive pointers.

Parameters:

other – Pointer to swap with.

STL support for intrusive_ptr.

namespace std
template<typename T>
struct hash<crimson_cell::intrusive_ptr<T>>

Hash support for intrusive_ptr.