1

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?

4

2 に答える 2

0

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.

于 2013-03-24T23:24:22.823 に答える
0

If you really want to do this you need all your objects to inherit from Spalloc with themselves as a template parameter, even your children classes :

class Child : public SPAlloc<Child>, public Base {
public:
    virtual ~Child() {};
public:
   using SPAlloc<Child>::Alloc;
};

You can also simply just use std::make_shared :)

于 2013-03-24T23:32:16.177 に答える