I'm trying to move things in and out from my thead safe deque:
template <typename T>
class ThreadSafeDeque
{
//..
T pop_front(void) noexcept
{
std::unique_lock<std::mutex> lock{_mutex};
while (_collection.empty())
{
_condNewData.wait(lock);
}
auto elem = std::move(_collection.front());
_collection.pop_front();
return elem;
}
private:
std::deque<T> _collection; // Concrete, not thread safe, storage.
//...
}
I created this class to insert into the Deque:
class DecodedFrame
{
public:
DecodedFrame(){}
DecodedFrame(const DecodedFrame &decodedFrame) = delete;
DecodedFrame &operator=(const DecodedFrame &) = delete;
std::unique_ptr<AVFrame, AVFrameDeleter> avFrame;
Now I'm trying to do
std::shared_ptr<ThreadSafeDeque<DecodedFrame>> decodedFramesFifo;
//add some `DecodedFrame`s to decodedFramesFifo
DecodedFrame decodedFrame = std::move(decodedFramesFifo->pop_front());
But the compiler complains that I deleted the copy assignment constructor, even though I'm trying to use the move assingment constructor. My guess is that it happens because pop_front
returns T
, not T&
. However, returning references makes no sense becaue the object is supposed to leave the deque forever and therefore the reference to it will die.
How can I move things here?
ps: how is it possible for the compiler to copy things when the DecodedFrame
holds an unique_ptr
? It can't be copied!
The problem is you declared your copy c'tor and assignment operator. Doesn't matter that the declaration deletes them, it's still a user supplied declaration. That suppresses the implicit declaration of the move operations. Your options are to
The copy-ctor/assign operations are deleted (these are also declarations) but that does not implicitly declare/define move-ctor/assign operations.
See p30 of https://fr.slideshare.net/ripplelabs/howard-hinnant-accu2014
You have to declare (default) them.
DecodedFrame(DecodedFrame &&) = default;
DecodedFrame &operator=(DecodedFrame &&) = default;
In order to avoid such frustrating behaviour, you should consider the rule of five.
(https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_five)
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