[英]Copying std::vector of primitive types behavior
Background: 背景:
When copying a std::vector
using its copy constructor or copy assignment like this: 使用其复制构造函数或复制赋值复制
std::vector
如下所示:
std::vector<T> v1{T(),T(),T()};
std::vector<T> v2 = v1;
a deep copy happens. 发生深层复制。
Is it guaranteed by the standard that the copy constructor of T
will be triggered for each element? 是否可以通过标准保证为每个元素触发
T
的复制构造函数? In other words no memcpy
(or something similar) will be called. 换句话说,不会调用
memcpy
(或类似的东西)。 (Correct me if I am wrong). (如果我错了,请纠正我)。
Question: 题:
On the other hand, is it guaranteed by the standard that it would call memcpy
(or something similar) on primitive types (for performance issues)? 另一方面,标准是否保证它会在原始类型上调用
memcpy
(或类似的东西)(针对性能问题)?
Yes, T
's copy constructor must be called. 是的,必须调用
T
的复制构造函数。 If the copy constructor is trivial, it's effect is the exact same as that of memcpy
, so an implementation is fine to use the latter to implement that copy constructor. 如果复制构造函数是微不足道的,它的效果与
memcpy
的效果完全相同,因此使用后者实现该复制构造函数的实现很好。 From there on, the implementation may decide to use memcpy
to implement the copy constructor of vector
. 从那时起,实现可能决定使用
memcpy
来实现vector
的复制构造函数。 That decision makes use of the as-if rule : The program's observable behavior remains unchanged, hence we need not actually perform all the copy constructor calls. 该决定使用as-if规则 :程序的可观察行为保持不变,因此我们不需要实际执行所有复制构造函数调用。
On the other hand, does it guaranteed by the standard that it would call memcpy (or something similar) on primitive types (for performance issues)?
另一方面,标准是否保证它会在原始类型上调用memcpy(或类似的东西)(针对性能问题)?
No, because how a copy constructor is actually implemented is an implementation detail. 不,因为实际上如何实现复制构造函数是一个实现细节。 The standard merely specifies a program's (observable) behavior, and it does that using the notion of copy constructors etc. Optimization is nothing an abstract standard document should worry about, but your vendor.
标准只是指定程序的(可观察的)行为,它使用复制构造函数等的概念来实现。优化不是抽象的标准文档应该担心的,而是您的供应商。 In fact, restraining the definitions of such functions would either incur a huge deficit in optimization, or be outright ignored due to the aforementioned as-if rule.
实际上,限制这些函数的定义会导致优化中的巨大缺陷,或者由于前面提到的as-if规则而被完全忽略。
The exact code generated for copy construction of primitive types is a quality of implementation issue. 为原始类型的复制构造生成的确切代码是实现质量问题。 In other words, the standard will not guarantee anything of the sort - at best, it will specify the algorithmic complexity of the operation, in this case copying of the vector can be inferred as
O(n)
. 换句话说,标准不保证任何类型 - 最多,它将指定操作的算法复杂性,在这种情况下,矢量的复制可以推断为
O(n)
。
With modern C++ compilers on a reasonable optimization setting, you can count on copy construction of POD classes being implemented in-line, as efficiently as a constant-size memcpy
. 使用现代C ++编译器进行合理的优化设置,您可以依赖于在线实现的POD类的复制构造,与常量大小的
memcpy
一样高效。 Doing anything else would incur serious penalties with typical use cases, such as STL containers of pointers. 做其他事情会导致典型用例的严重处罚,例如指针的STL容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.