问题描述
我目前正在尝试对管道进行编程,该管道能够处理每个管道元素中的不同类型的数据。现在,我想将unique_ptr
与某种模板多态性和模板专业化配合使用。
struct Start {}; // Dummy struct
template<typename In,typename Out>
class PipelineElement {
public:
virtual Out process(In in) = 0;
};
// partial template specialization for the first pipeline element
template<typename Out>
class PipelineElement<Start,Out> {
public:
virtual Out process() = 0;
};
class Producer : public PipelineElement<Start,int> {
int process() override { ... }
};
现在,函数应采用部分专用unique_ptr
中的PipelineElement
。但是,以下内容不会与错误消息一起编译:
自动管道:: setStart
(std :: unique_ptr ,std :: default_delete >>)':无法从'std :: unique_ptr转换参数1 >'到'std :: unique_ptr ,std :: default_delete >>
class Pipeline {
template<typename Out>
static auto setStart(std::unique_ptr<PipelineElement<Start,Out>> element) { ... }
};
int main() {
Pipeline::setStart(std::make_unique<Producer>());
}
如果我改用常规指针,它可以编译而不会出现任何错误。
为什么带有普通指针的版本不能编译,而带有智能指针的版本不能编译?
class Pipeline {
template<typename Out>
auto setStart(PipelineElement<Start,Out>* element) { ... }
};
int main() {
Pipeline::setStart(new Producer());
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)