So I was browsing a rather strange C++ codebase (a simple game engine), and came across something like this:
std::shared_ptr<Storage> handler_for_memstor {allocated, m_raw_handle};
I wonder what that is, and checked reference, found a constructor that looks like it. The signature of that constructors for std::shared_ptr
is as follows:
template< class Y >
shared_ptr( const shared_ptr<Y>& r, element_type* ptr ) noexcept;
According to the cppreference site:
constructs a shared_ptr which shares ownership information with the initial value of r, but holds an unrelated and unmanaged pointer ptr. If this shared_ptr is the last of the group to go out of scope, it will call the stored deleter for the object originally managed by r. However, calling get() on this shared_ptr will always return a copy of ptr. It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by r or is an alias (e.g., downcast) of r.get()
However, I can not imagine a usage for this constructor. Why would anyone want to bind an unmanaged pointer to the lifetime of another?