Skip to content

LiteFX::SharedObject::weak_from_this

Returns a weak pointer to the current object instance.

Synopsis

Declared in <litefx/containers.hpp>

template<typename TSelf>
[[nodiscard]] WeakPtr<std::remove_reference_t<TSelf>> weak_from_this(this TSelf&& self) noexcept;

Description

Note that there's a subtle difference between the standard version and this implementation. Since there are no typed casts for weak pointers, internally this function acquires a temporary reference for the object, which only works if the object is not fully released. In practice, this is almost never an issue, except for an additional atomic increment/decrement. If the parent pointer is already released, this returns an empty pointer too. The edge case exists where the pointer is released, but the object is still alive. For example, when calling this function from a destructor of a derived class, different to the standard version, this hands out an empty pointer, while the standard would return an expired, but non-empty pointer.

Return Value

a weak pointer to the current object instance.

Note

The return value should not be discarded.