template <typename T>注釋下方的地方會不會出現head先釋放的情況
class Queue
{
private:
struct Node
{
T data_;
std::unique_ptr<Node> next_;
Node(T data) : data_(std::move(data)) {}
};
std::unique_ptr<Node> head_{ nullptr };
Node *tail_{ nullptr };
public:
std::shared_ptr<T> try_pop()
{
if (head_ == nullptr)
return std::shared_ptr<T>();
std::shared_ptr<T> res(std::make_shared<T>(std::move(head_->data_)));
//std::unique_ptr<Node> old_head = std::move(head_);
//head_ = std::move(old_head_->next_);
head_ = std::move(head_->next_);
return res;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/183343.html
標籤:C++ 語言
上一篇:復印機
下一篇:哪位大佬教我此題
