weak_ref, strong_ref: Reference Holding in C++ (Updated)

Published December 12, 2025

weak_ref, strong_ref: Reference Holding in C++ (Updated)

I: Abstract

Nectar has introduced two new primitives for Reference holding, WeakRef and StrongRef. The idea is to separate references as strong and weak references.

This extends and deprecates the Ref pattern in Nectar.

II: Principle

You should be able to hold references to a pointer without having to own it. That is the purpose of a WeakRef, however the StrongRef does the opposite. StrongRef mandates that the pointer may be deleted by the class itself to guarantee RAII.

However this is heavily context dependent and shall respect the codebase guidelines and its software design rules.

Bonus: WeakRef

WeakRef<AstLeaf> refLeaf{weakAst};
/** usage... */
/** refLeaf is not freed! */

Bonus: StrongRef

StrongRef<DLLLoader> dllRef{dll};
/** usage... */
/** dllRef is freed! */

Note: On NonNullRef

NonNullRef shall be a strong reference by default, as referencing a weak reference that may be null defeats the purpose of a NonNull container.

References