Benjamin Cecchetto
Benjamin Cecchetto

Reputation: 85

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?...

Upvotes: 4

Views: 2936

Answers (1)

Praetorian
Praetorian

Reputation: 109159

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);
}

Upvotes: 3

Related Questions