I am trying to learn how shared_ptr works by implementing it from scratch, and I can't figure out how to detect T's base class.
I've tried using is_base_of(), but that gives a const value, which I can't use with an if statement to set the object's internal weak_ptr.
I was thinking along these lines:
template <class T>
class shared_ptr
{
shared_ptr(T* ptr)
{
...
}
shared_ptr(enable_shared_from_this<T>* ptr)
{
...
Ptr->m_this = weak_ptr<T>(this);
}
};
but no luck so far. Boost's and VC++ implementations are too confusing for me, I'm looking for a simple explanation.
Here it says
The constructors of std::shared_ptr detect the presence of an enable_shared_from_this base and assign the newly created std::shared_ptr to the internally stored weak reference.
Yeah, how?