返回 unique_ptr 私有成员数据而不转移所有权
Returning unique_ptr private member data without transferring ownership
上下文
下面的错误似乎是在告诉我,我无法从这个 get 函数 return 我的 unique_ptr
调用 m_head
。我只想 return 我的 unique_ptr
m_head
而不转让所有权。
自从引入智能指针以来,我一直在完全避免使用原始指针,因为原始指针不是异常安全的,有内存管理开销和我已经意识到的其他问题。也许有这样的情况我应该在一个小范围内简单地使用它们?
在这方面,我认为我需要转移所有权,而不是我目前的方法。我应该改为获取由 unique_ptr
管理的对象,创建一个新的 shared_ptr
来管理该对象,然后 return shared_ptr
,但需要一些确认。我认为可能是这种情况,因为 std::unique_ptr 文档说:
unique_ptr objects own their pointer uniquely: no other facility shall
take care of deleting the object, and thus no other managed pointer
should point to its managed object, since as soon as they have to,
unique_ptr objects delete their managed object without taking into
account whether other pointers still point to the same object or not,
and thus leaving any other pointers that point there as pointing to an
invalid location.
错误
`function "std::unique_ptr<_Ty, _Dx>::unique_ptr(const std::unique_ptr<_Ty, _Dx> &)
[with _Ty=mrobsmart::LinkedList::Node, _Dx=std::default_delete<mrobsmart::LinkedList::Node>]"
(declared at line 2337 of "C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\VC\Tools\MSVC.15.26726\include\memory")
cannot be referenced -- it is a deleted function
代码
#include <memory>
class LinkedList
{
private:
std::unique_ptr<Node> m_head;
public:
LinkedList(int data) {
m_head = std::make_unique<Node>(data);
}
const std::unique_ptr<Node> get_head() { return m_head; }
};
I just want to return my unique_ptr m_head without transferring ownership.
这不可能。 unique_ptr
是围绕每一步都转移其所有权的行为而设计的。
Note, I've been avoiding raw pointers completely since introduced to smart pointers since raw pointers are not exception safe, have the memory management overhead and other issues I've been aware of, but maybe there are cases like this where I should use them briefly contained in a small scope?
原始指针并不邪恶。将它们用作纯 references/indirections 是一个完全有效的用例——不涉及所有权、内存管理或异常安全。
当然,return C++ 参考也是可能的。选择指针还是引用可以看值是否可以为null,但归根结底也是代码风格的问题。
因此,其中任何一个(重要:const
-限定函数):
const Node* get_head() const { return m_head.get(); }
const Node& get_head() const { return *m_head; }
上下文
下面的错误似乎是在告诉我,我无法从这个 get 函数 return 我的 unique_ptr
调用 m_head
。我只想 return 我的 unique_ptr
m_head
而不转让所有权。
自从引入智能指针以来,我一直在完全避免使用原始指针,因为原始指针不是异常安全的,有内存管理开销和我已经意识到的其他问题。也许有这样的情况我应该在一个小范围内简单地使用它们?
在这方面,我认为我需要转移所有权,而不是我目前的方法。我应该改为获取由 unique_ptr
管理的对象,创建一个新的 shared_ptr
来管理该对象,然后 return shared_ptr
,但需要一些确认。我认为可能是这种情况,因为 std::unique_ptr 文档说:
unique_ptr objects own their pointer uniquely: no other facility shall take care of deleting the object, and thus no other managed pointer should point to its managed object, since as soon as they have to, unique_ptr objects delete their managed object without taking into account whether other pointers still point to the same object or not, and thus leaving any other pointers that point there as pointing to an invalid location.
错误
`function "std::unique_ptr<_Ty, _Dx>::unique_ptr(const std::unique_ptr<_Ty, _Dx> &)
[with _Ty=mrobsmart::LinkedList::Node, _Dx=std::default_delete<mrobsmart::LinkedList::Node>]"
(declared at line 2337 of "C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\VC\Tools\MSVC.15.26726\include\memory")
cannot be referenced -- it is a deleted function
代码
#include <memory>
class LinkedList
{
private:
std::unique_ptr<Node> m_head;
public:
LinkedList(int data) {
m_head = std::make_unique<Node>(data);
}
const std::unique_ptr<Node> get_head() { return m_head; }
};
I just want to return my unique_ptr m_head without transferring ownership.
这不可能。 unique_ptr
是围绕每一步都转移其所有权的行为而设计的。
Note, I've been avoiding raw pointers completely since introduced to smart pointers since raw pointers are not exception safe, have the memory management overhead and other issues I've been aware of, but maybe there are cases like this where I should use them briefly contained in a small scope?
原始指针并不邪恶。将它们用作纯 references/indirections 是一个完全有效的用例——不涉及所有权、内存管理或异常安全。
当然,return C++ 参考也是可能的。选择指针还是引用可以看值是否可以为null,但归根结底也是代码风格的问题。
因此,其中任何一个(重要:const
-限定函数):
const Node* get_head() const { return m_head.get(); }
const Node& get_head() const { return *m_head; }