Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make_shared with template constructor

I have a class with a template constructor, and want to have a shared_pointer to it. Such as:

class A;
typedef std::shared_ptr<A> A_ptr;

class A {
public:
    template <typename T>
    A(Arg1 arg1, Arg2 arg2, T data) { ... do something with data ... }

    static A_ptr New(Arg1 arg1, Arg2 arg2, T data) {
        return make_shared<A>(arg1,arg2,data);
    }
};

I know the line for the make_shared isn't right, but don't know the solution. Is it even possible to call make_shared with a template constructor? Do I need to make the 'New' function a template one and pass in A<T> somehow?...

like image 646
Benjamin Cecchetto Avatar asked Oct 20 '25 11:10

Benjamin Cecchetto


1 Answers

Convert A::New into a function template

template<typename T>
static A_ptr New(Arg1 arg1, Arg2 arg2, T data) {
    return std::make_shared<A>(arg1,arg2,data);
}
like image 83
Praetorian Avatar answered Oct 23 '25 01:10

Praetorian