与unique_ptr相结合的升压ptr_vector错误

Error with boost ptr_vector combined with unique_ptr

本文关键字:ptr vector 错误 相结合 unique      更新时间:2023-10-16

我试图从我的代码中删除auto_ptr。但是我收到此错误,不知道为什么?


no matching function for call to 'boost::ptr_vector<a>::push_back(std::remove_reference<std::unique_ptr<a>&>::type)'
note: candidates are:
... note: 'std::unique_ptr<a>' is not derived from 'std::auto_ptr<T>'

{
    boost::ptr_vector<a>& c;
    std::unique_ptr<a> b( new a(x, y) );
    if (!b->isValid())
        return;
    c.push_back(std::move(a));
}
这正是

错误消息告诉您的:boost::ptr_vector::push_back期望auto_ptr,但您提供了一个无法转换为auto_ptrunique_ptr

由于您要转换为 std::unique_ptr ,您将不再需要boost::ptr_vector,因为它只是作为auto_ptr的解决方法,无法存储在容器中。请改用std::vector<std::unique_ptr<T>> .