简体   繁体   English

使用模板构造函数make_shared

[英]make_shared with template constructor

I have a class with a template constructor, and want to have a shared_pointer to it. 我有一个带有模板构造函数的类,并希望有一个shared_pointer 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. 我知道make_shared的行不对,但不知道解决方案。 Is it even possible to call make_shared with a template constructor? 是否可以使用模板构造函数调用make_shared Do I need to make the 'New' function a template one and pass in A<T> somehow?... 我是否需要将“新”功能设为模板并以某种方式传入A<T> ?...

Convert A::New into a function template A::New转换为函数模板

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM