[英]Why/when should I use std::unique/shared_ptr (std::vector<>) over just std::vector<>?
I'm a little bit confused about the main use of std::unique/shared_ptr(std::vector<>)
when I can simply use a std::vector<>
, which, as I know, is itself inherently a dynamic array. 当我可以简单地使用
std::vector<>
时,我对std::unique/shared_ptr(std::vector<>)
的主要用法有些困惑,据我所知,它本身就是动态的数组。 As I have also seen around, people say that there is no any performance difference between these two. 正如我在周围所看到的,人们说这两者之间没有任何性能差异。 So, based on all this, what is the point of using a smart pointer pointing to a container (in this case, a vector) instead of a vector alone?
因此,基于所有这些,使用指向容器(在本例中为向量)而不是向量的智能指针有什么意义?
First of all, you shouldn't be using std::shared_ptr
unless you need the specific "shared ownership" semantics associated with std::shared_ptr
. 首先,你不应该使用
std::shared_ptr
,除非你需要特定的关联“共同拥有”语义std::shared_ptr
。 If you need a smart pointer, you should default to std::unique_ptr
by default, and only switch away from it in the scenario where you expressly find that you need to. 如果您需要智能指针,则默认情况下应默认使用
std::unique_ptr
,并且仅在明确发现需std::unique_ptr
的情况下才将其切换。
Secondly: ostensibly , the reason to prefer std::unique_ptr<TYPE>
over TYPE
is if you plan to move the object around a lot. 其次: 表面上看 ,首选
std::unique_ptr<TYPE>
不是TYPE
是如果您计划将对象移动很多。 This is the common design paradigm for large objects that are either unmovable, or otherwise expensive to move—ie they implemented a Copy Constructor and didn't implement a Move Constructor, so moves are forced to behave like a Copy. 这是不可移动或移动昂贵的大型对象的常见设计范例,即大型对象实现了复制构造函数而未实现移动构造函数,因此移动被迫表现得像“复制”。
std::vector
, however, does have relatively efficient move semantics: if you move a std::vector
around, regardless of how complex its contained types are, the move only constitutes a couple of pointer swaps. 但是,
std::vector
确实具有相对有效的移动语义:如果左右移动std::vector
,无论其所包含的类型有多复杂,该移动仅构成几个指针交换。 There's no real risk that moving a std::vector
will incur a large amount of computational complexity. 移动
std::vector
不会引起大量的计算复杂性,这没有真正的风险。 Even in the scenario where you're overriding a previously allocated array (invoking the Destructors of all objects in the vector), you'd still have that complexity if you were using std::unique_ptr<std::vector<TYPE>>
instead, saving you nothing. 即使在覆盖先前分配的数组(调用向量中所有对象的析构函数)的情况下,如果您使用的是
std::unique_ptr<std::vector<TYPE>>
,仍然会遇到这种复杂性,什么都不省。
There are two advantages to std::unique_ptr<std::vector<TYPE>>
. std::unique_ptr<std::vector<TYPE>>
有两个优点。 The first of which is that it gets rid of the implicit copy constructor; 第一个是它摆脱了隐式副本构造函数。 maybe you want to enforce to maintaining programmers that the object shouldn't be copied.
也许您想强制维护程序员不要复制该对象。 But that's a pretty niche use.
但这是相当利基的用途。 The other advantage is that it allows you to stipulate the scenario where there's no vector, ie
vec.size() == 0
is a different condition than doesNotExist(vec)
. 另一个优点是,它允许您规定没有向量的情况,即
vec.size() == 0
是与doesNotExist(vec)
不同的条件。 But even in that scenario, you should be preferring std::optional<std::vector>
instead, which better conveys through the code the intent of the object. 但是即使在这种情况下,您也应该更喜欢
std::optional<std::vector>
,它可以更好地通过代码传达对象的意图。 Granted, std::optional
is only available in C++17→ Code, so maybe you're in an environment that hasn't implemented it yet. 授予的
std::optional
仅在C ++ 17→代码中可用,因此也许您处于尚未实现它的环境中。 But otherwise, there's little reason to use std::unique_ptr<std::vector>
. 但是否则,没有理由使用
std::unique_ptr<std::vector>
。
So in general, I don't believe there are practical uses for std::unique_ptr<std::vector>
. 因此,总的来说,我认为
std::unique_ptr<std::vector>
没有实际用途。 There's no practical performance difference between it and std::vector
, and using it will just make your code needlessly complex. 它和
std::vector
之间没有实际的性能差异,使用它只会使您的代码不必要地复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.