C++ unique_ptr 右值传递使用 std::move 不起作用

C++ unique_ptr rvalue passing using std::move not working

error: call to implicitly-deleted copy of 'unique_ptr<Searchable>'                                                                      
unique_ptr<Searchable> current(std::move(searchSpace.top()));

你好,

我想弄清楚为什么这不能编译。搜索 space 是优先队列,我正在尝试将右值 searchSpace.top() 移动到新的当前对象。

奇怪的是,如果我将优先级队列更改为双端队列:

        unique_ptr<Searchable> current(std::move(Q.front()));

这在 Q 是双端队列时工作正常。

那么为什么后一个工作而不是第一个工作?

错误消息的其余部分:

 copy constructor is implicitly deleted because 'unique_ptr<Searchable,
  std::__1::default_delete<Searchable> >' has a user-declared move
  constructor
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT  

std::priority_queue::top() 只有一个 const 重载返回一个 const 引用。这不能从中移动。

另一方面,

std::deque::front() 有 const 和非常量重载返回同样合格的引用。您似乎正在使用非常量重载,其结果可以从中移动。

根据documentation of std::priority_queue::top()

const_reference top() const;

它 returns const 引用,std::move 将被转换为 const T && 。另一方面,const T && 不被 std::unqiue_ptr 的 move ctor 接受,因为它没有这样的重载,它只接受 T &&(没有 const)见 documentation of std::unqiue_ptr ctors

为什么 const 引用被 std::move 接受,请参阅此处