Search code examples
c++c++11stdshared-ptr

How is std::shared_ptr polymorphic?


I'm developing some container class, which has similar idea: holding pointer inside.

#include <iostream>

template<class T>
class Container
{
public:
    Container ( )
    {
         pointer = new T ( );
    }

    ~Container ( )
    {
        delete pointer;
    }

    T* operator->( )
    {
        return pointer;
    }
private:
     T* pointer;
};

struct Base
{
    virtual void who ( )
    {
        std::cout << "Base" << std::endl;
    }
};


struct Child : public Base
{
    virtual void who ( ) override
    {
        std::cout << "Child" << std::endl;
    }
};

void testContainer ( Container<Base> c )
{
    c->who ( );
}

void testSharedPtr ( std::shared_ptr<Base> s )
{
    s->who ( );
}

int main ( )
{
    Container<Child> child;
    std::shared_ptr<Child> sharedChild;

    testSharedPtr ( sharedChild );
    testContainer ( child );
}

This code fails to compile: error C2664: 'void test(Container<Base> &)' : cannot convert argument 1 from 'Container<Child>' to 'Container<Base> &'
However with std::shared_ptr<Base>, instead of Container, everything works fine. So question is:
Is std::shared_ptr-like polymorphysm implementable? Or is this feature, which was somehow hardcoded in C++? Sorry for my primitive language.


Solution

  • Implement the following constructor:

    template<class Other,
             class = typename std::enable_if<std::is_convertible<Other*, T*>::value>::type>
    Container(const Container<Other>& _Other) 
    {
        pointer = _Other.pointer;
    }
    

    This constructor will only be enabled if Other* is convertible to T*, which is obviosly the case if T is the base class of Other.