shared_ptr

A shared pointer

Shared pointers are a form of automatic reference counting. An internal heap-allocated object stores a reference to the refcounted object.

You may borrow weak references from shared_ptr, these weak references may become invalid at any point, so make sure to check the state of the object using isValid.

Constructors

this
this(inout(typeof(this)) src)

Copy Constructor.

this
this(Ref!T ptr)

Constructs a shared_ptr with the given memory address.

Destructor

~this
~this()

Destructor.

Alias This

value

Members

Functions

borrow
weak_ptr!T borrow()

Borrows a weak reference from the shared pointer.

Properties

isValid
bool isValid [@property getter]

Whether the smart pointer value is still valid.

value
T value [@property getter]

The value stored within the smart pointer.

Examples

auto wp = shared_new!int(42);
if (wp.isValid) {
    // Use the value.
}

Threadsafety: The internal reference count kept by shared_ptr is not atomic. As such you should take care with accessing the value and refcount stored within.

See Also

nulib.memory.unique_ptr.unique_ptr nulib.memory.weak_ptr.weak_ptr

Meta