Get the latest tech news
Qt and C++ Trivial Relocation (Part 1)
Discover how Qt optimizes container operations with byte-level manipulations, including trivial relocation for types like int and QString.
Depending on the operation that causes the reallocation ( push_back, insert in the middle, simple reserve) a new element may also be added to the just allocated block of memory; that’s not really relevant here. The consequence is that the reference counter of the string does not need to change; in the end the total number of QString objects pointing to that payload is the same. While it works for things like int, QPoint, QString, or QPen, this optimization is not safe to do in general: for some types Qt cannot replace move construction + destruction with a memcpy.
Or read this on Hacker News