gpt4 book ai didi

c++ - 即使我正在移动,编译器也会尝试使用复制构造函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:58 24 4
gpt4 key购买 nike

我正在尝试将东西从我的 thead 安全双端队列中移入移出:

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.
//...
}

我创建了这个类来插入到双端队列中:

class DecodedFrame
{
public:
DecodedFrame(){}
DecodedFrame(const DecodedFrame &decodedFrame) = delete;
DecodedFrame &operator=(const DecodedFrame &) = delete;
std::unique_ptr<AVFrame, AVFrameDeleter> avFrame;

现在我正在尝试做

std::shared_ptr<ThreadSafeDeque<DecodedFrame>> decodedFramesFifo;
//add some `DecodedFrame`s to decodedFramesFifo
DecodedFrame decodedFrame = std::move(decodedFramesFifo->pop_front());

但是编译器提示我删除了复制赋值构造函数,即使我正在尝试使用移动赋值构造函数。我猜这是因为 pop_front 返回 T,而不是 T&。然而,返回引用是没有意义的,因为对象应该永远离开双端队列,因此对它的引用将会消失。

我怎样才能把东西移到这里?

ps:当 DecodedFrame 持有 unique_ptr 时,编译器怎么可能复制东西?不可复制!

最佳答案

问题是你声明了你的复制c'tor和赋值运算符。声明删除它们并不重要,它仍然是用户提供的声明。这抑制了移动操作的隐式声明。您的选择是

  1. 明确默认移动操作。
  2. 删除复制操作声明,由于不可复制成员,它们仍将被隐式删除。

关于c++ - 即使我正在移动,编译器也会尝试使用复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57705918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com