summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qvector.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qvector.h')
-rw-r--r--src/corelib/tools/qvector.h27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h
index 6acbf3561e..85393a3598 100644
--- a/src/corelib/tools/qvector.h
+++ b/src/corelib/tools/qvector.h
@@ -835,18 +835,25 @@ typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, c
if (!isDetached() || d->size + n > int(d->alloc))
realloc(d->size + n, QArrayData::Grow);
if (!QTypeInfoQuery<T>::isRelocatable) {
- T *b = d->end();
- T *i = d->end() + n;
- while (i != b)
- new (--i) T;
- i = d->end();
+ T *const e = d->end();
+ T *const b = d->begin() + offset;
+
+ T *i = e;
T *j = i + n;
- b = d->begin() + offset;
- while (i != b)
- *--j = *--i;
- i = b+n;
+
+ // move old elements into the uninitialized space
+ while (i != b && j > e)
+ new (--j) T(std::move(*--i));
+ // move the rest of old elements into the tail using assignment
while (i != b)
- *--i = copy;
+ *--j = std::move(*--i);
+
+ // construct copies of t inside the uninitialized space
+ while (j != b && j > e)
+ new (--j) T(copy);
+ // use assignment to fill the recently-moved-from space
+ while (j != b)
+ *--j = copy;
} else {
T *b = d->begin() + offset;
T *i = b + n;