微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 从优先级队列中获取unique_ptr

我在priority_queue中维护一组unique_ptr实例.在某些时候,我想获得第一个元素并将其从队列中删除.但是,这总是会产生编译器错误.请参阅下面的示例代码
int main ()
{
  std::priority_queue<std::unique_ptr<int>> queue;
  queue.push(std::unique_ptr<int>(new int(42)));

  std::unique_ptr<int> myInt = std::move(queue.top());
  return 1;
}

这会产生以下编译器错误(gcc 4.8.0):

uptrtest.cpp: In function ‘int main()’: uptrtest.cpp:6:53: error: use of deleted function ‘std::unique_ptr<_Tp,_Dp>::unique_ptr(const std::unique_ptr<_Tp,_Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’    std::unique_ptr<int> myInt = std::move(queue.top());
                                                     ^ In file included from /usr/include/c++/4.8/memory:81:0,from uptrtest.cpp:1: /usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^

this question一样更改代码使用队列修复了问题,代码编译得很好.

有没有办法将unique_ptrs保留在priority_queue中,或者我错过了什么?

解决方法

std :: priority_queue :: top()返回一个const引用,因此您无法移动它.查看 public interface of priority_queue,没有方法可以获取可以移动的非const引用(这对于unique_ptr是必需的,它没有复制构造函数).

解决方案:将unique_ptr替换为shared_ptr以便能够复制它们(而不仅仅是移动它们).

或者,当然,完全使用另一种容器(但如果你首先选择priority_queue,这可能是你不能接受的).

您也可以使用“受保护的成员黑客”访问受保护的成员c(底层容器),但我不推荐它,这很脏,很可能是UB.

原文地址:https://www.jb51.cc/c/114527.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐