“可复制构造"C++ stl 容器元素的要求

关于C++ stl容器元素的要求,标准规定:元素类型应为CopyConstructible,并有CopyConstructible要求表.同样通过各种书籍(Josuttis 等),生成的副本应该与源等效".

Regarding to the requirement for C++ stl container element, the standard says: the element type should be CopyConstructible, and there is a table for CopyConstructible requirements. Also by various books (Josuttis, etc.), the generated copy should be "equivalent to" the source.

我想我需要澄清一下.究竟什么是等同于"?我也对CopyConstructible"和深/浅拷贝"之间的关系有点困惑.一般来说,拷贝构造函数要么是浅拷贝,要么是深拷贝.那么哪个适用于CopyConstructible",哪个不适用?

I think I need some clarity here. What is exactly "equivalent to"? Also I am a bit confused with the relation between the "CopyConstructible" and the "deep/shallow copy". In general, a copy constructor is either shallow copy or deep copy. So which one applies to the "CopyConstructible", and which does not?

感谢您的任何评论!

推荐答案

深拷贝和浅拷贝都可以.例如,shared_ptr 总是做一个浅拷贝(带有一些额外的引用计数的东西),你可以在容器中使用它们就好了.这取决于复制操作的语义.

Deep or shallow copy both work. For instance, shared_ptr always does a shallow copy (with some extra reference counting stuff), and you can use them in containers just fine. It depends on the semantics of copy-operation.

等效意味着您的程序不应依赖于它是否适用于原件或副本.

Equivalent means your program should not depend on whether it works with the original or with the copy.

相关文章