boost ptr_vector与unique_ptr结合使用时出错

时间:2015-08-17 09:54:38

标签: c++ c++11 boost

我正试图从我的代码中删除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));
}

1 个答案:

答案 0 :(得分:6)

这正是错误消息告诉您的内容:boost::ptr_vector::push_back需要auto_ptr,但您提供的unique_ptr无法转换为auto_ptr

由于您要转换为std::unique_ptr,因此您不再需要boost::ptr_vector,因为它仅仅是auto_ptr的解决方法,无法存储在容器中。而是使用std::vector<std::unique_ptr<T>>