如何在不夺走容器所有权的情况下访问容器的 unique_ptr 元素(通过迭代器)?当获得容器中某个元素的迭代器时,元素所有权是否仍属于容器?当人们取消引用迭代器以获得对 unique_ptr 的访问时怎么样?这是否执行 unique_ptr 的隐式移动?
我发现当我需要在容器中存储元素(而不是按值)时,我经常使用shared_ptr,即使容器概念上拥有这些元素,而其他代码只是希望操作容器中的元素,因为我担心在不夺取容器所有权的情况下,无法实际访问容器中的 unique_ptr 元素。
有什么见解吗?
How does one access unique_ptr elements of a container (via an iterator) without taking ownership away from the container? When one gets an iterator to an element in the container is the element ownership still with the container? How about when one dereferences the iterator to gain access to the unique_ptr? Does that perform an implicit move of the unique_ptr?
I find I'm using shared_ptr a lot when I need to store elements in a container (not by value), even if the container conceptually owns the elements and other code simply wishes to manipulate elements in the container, because I'm afraid of not being able to actually access the unique_ptr elements in the container without ownership being taken from it.
Any insights?
发布评论
评论(2)
使用
auto
和 C++11 基于范围的 for 循环,这变得相对优雅:对
std::unique_ptr
的引用&
> 避免复制,您可以使用uniqe_ptr
而无需取消引用。With
auto
and the range-based for-loops of C++11 this becomes relatively elegant:The reference
&
to thestd::unique_ptr
avoids the copying and you can use theuniqe_ptr
without dereferencing.只要您不尝试复制
unique_ptr
,就可以使用它。您必须“双重取消引用”迭代器才能获取指针的值,就像使用shared_ptr
所做的那样。这是一个简短的示例:如果您(不小心)制作了
unique_ptr
的副本:那么您将在编译时找到答案。它不会导致运行时错误。您的用例就是构建
container>
的原因。事情应该正常工作,如果不正常,问题会出现在编译时而不是运行时。因此,请继续编写代码,如果您不明白编译时错误,请回到此处提出另一个问题。As long as you don't try to make a copy of the
unique_ptr
, you can just use it. You'll have to "double dereference" the iterator to get to the pointer's value, just as you would have to withshared_ptr
. Here's a brief example:If you do (accidentally) make a copy of the
unique_ptr
:then you'll find out at compile time. It won't cause a run time error. Your use case is why
container<unique_ptr<T>>
was built. Things should just work, and if they don't, the problem appears at compile time instead of run time. So code away, and if you don't understand the compile time error, then ask another question back here.