0

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?

sorush-r
  • 10,490
  • 17
  • 89
  • 173
  • @L.F. Actually, I've read that and still don't get it! Especially because the types do not match the signature in `shared_ptr specific_data(f, &f->bar);` – sorush-r Feb 16 '20 at 10:54
  • Oh I get it now (: It actually matches the signature. don't mind my question. – sorush-r Feb 16 '20 at 10:59

0 Answers0