I am maintaining a set of unique_ptr
instances in a priority_queue
. At some point, I want to get the first element and remove it from the queue. However, this always produces a compiler error. See sample code below.
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;
}
This produces the following compiler error (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;
^
Changing the code to use queue
like in this question fixes the issue and the code compiles just fine.
Is there no way to keep unique_ptr
s in a priority_queue
or am I missing something?
A unique_ptr can only be moved. This means that the ownership of the memory resource is transferred to another unique_ptr and the original unique_ptr no longer owns it. We recommend that you restrict an object to one owner, because multiple ownership adds complexity to the program logic.
std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.
Most priority queue implementations, including the C++ std::priority_queue type, don't support random access. The idea behind a priority queue is to sacrifice random access for fast access to the smallest element. Depending on what you're trying to do, there are a number of other approaches you could use.
std::priority_queue::top()
returns a const reference so you can't move it. Looking at the public interface of priority_queue
there is no method to get a non-const reference that you can move (which is mandatory for unique_ptr
, it has no copy constructor).
Solution: replace unique_ptr
with shared_ptr
to be able to copy them (and not just move them).
Or, of course, use another kind of container altogether (but if you chose priority_queue
in the first place, this is probably not acceptable for you).
You could also maybe use a "protected member hack" to access the protected member c
(the underlying container) but I wouldn't recommend it, this is quite dirty and quite probably UB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With