开发者

Using memcpy in the STL

开发者 https://www.devze.com 2023-01-02 14:53 出处:网络
Why does C++\'s vector class call copy constructors? Why doesn\'t it just memcpy the underlying data? Wouldn\'t that be a lot faster, and remove half of the need for move semantics?

Why does C++'s vector class call copy constructors? Why doesn't it just memcpy the underlying data? Wouldn't that be a lot faster, and remove half of the need for move semantics?

I can't imagine a use case where this would be worse, but then again, maybe it's just because I'm being quite unimag开发者_JS百科inative.


Because the object needs to be notified that it is being moved. For example, there could be pointers to that given object that need to be fixed because the object is being copied. Or the reference count on a reference counted smart pointer might need to be updated. Or....

If you just memcpy'd the underlying memory, then you'd end up calling the destructor twice on the same object, which is also bad. What if the destructor controls something like an OS file handle?

EDIT: To sum up the above: The copy constructor and destructor can have side effects. Those side effects need to be preserved.


You can safely memcpy POD and built-in types. Those are defined to have no semantics beyond being a bag of bits with respect to memory.

Types that have constructors and destructors, however, must be constructed and destructed in order to maintain invariants. While you may be able to create types that can be memcpy'd and have constructors, it is very difficult to express that in the type signature. So instead of possibly violating invariants associated with all types that have constructors and/or destructors, STL errs on the side of instead maintaining invariants by using the copy constructor when necessary.

This is why C++0x added move semantics via rvalue references. It means that STL containers can take advantage of types that provide a move constructor and give the compiler enough information to optimize out otherwise expensive construction.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号