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

Having a single method in a base class able to allocate for child classes


I'm trying to have a common base/helper class that allocates shared_ptrs for the calling class, but I'm having problems getting it to work in derived classes.

#include <memory>

template<typename T>
struct SPAlloc {

    virtual ~SPAlloc() {}

    template<typename ...Args>
    static std::shared_ptr<T>
    Alloc(Args&&... params) {
        return std::make_shared<T>(std::forward<Args>(params)...);
    }


    template<class U, typename ...Args>
    static std::shared_ptr<U>
    Alloc(Args&&... params) {
        return std::make_shared<U>(std::forward<Args>(params)...);
    }
};

class Base : public SPAlloc<Base> {
public:
    virtual ~Base() {};
};

class Child : public Base {
public:
    virtual ~Child() {};
};

typedef std::shared_ptr<Base> pBase;
typedef std::shared_ptr<Child> pChild;

int main() {
    pBase base = Base::Alloc();
    pChild child = Child::Alloc();
}

I understand that the class Base : public SPAlloc<Base> means that T in the template is going to be Base, which is why I created the second Alloc. The second alloc needs to be called like Child::Alloc<Child>().

Is there a way to write this Alloc so that the compiler can deduce the class I'm calling Alloc with?


Solution

  • Short answer: No, there isn't.

    Long answer: The point is that Alloc is not aware of Child unless explicitly told, so where would that information come from? The call to Child::Alloc() is a call to Base::Alloc(), which is a call to SPAlloc<Base>::Alloc() and there, all information about Child is lost.

    The easiest solution is using a free function, but that function already exists and it's called: std::make_shared. Maybe consider using it directly and safe yourself the trouble with SPAlloc altogether.

    Alternatively, if you want to overwrite SPAlloc<T>::Alloc() for each child, you won't need the base class SPAlloc at all, just add the method to each class, that's probably easier than using a base class.