c++booststdshared-ptrenable-shared-from-this

How does shared_ptr<T> detect that T derives from enable_shared_from_this<T>?


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?


Solution

  • Simple - use template argument deduction! That's the solution to all the world's problems, but you knew that already :) A solution based on the way boost solves your problem is below. We create a templated helper class which actually handles the details of the construction.

    template <class T>
    class shared_ptr
    {
        shared_ptr(T* ptr)
        {
            magic_construct(this, ptr, ptr);
        }
    };
    
    template <class X, class Y, class Z>
    void magic_construct(shared_ptr<X>* sp, Y* rp, enable_shared_from_this<Z>* shareable)
    {
    //Do the weak_ptr handling here
    }
    
    void magic_construct(...)//This is the default case
    {
    //This is the case where you have no inheritance from enable_shared_from_this
    }