繁体   English   中英

从队列中删除unique_ptr

[英]remove unique_ptr from queue

我试图找出如何/如果我可以在queue使用unique_ptr

// create queue
std::queue<std::unique_ptr<int>> q;

// add element
std::unique_ptr<int> p (new int{123});
q.push(std::move(p));

// try to grab the element
auto p2 = foo_queue.front();
q.pop(); 

我明白为什么上面的代码不起作用。 由于frontpop是两个单独的步骤,因此元素无法移动。 有没有办法做到这一点?

你应该明确地说要移动指针从队列中 像这样:

std::unique_ptr<int> p2 = std::move(q.front());
q.pop();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM