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?