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.h29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h
index d61c26dc2e..85393a3598 100644
--- a/src/corelib/tools/qvector.h
+++ b/src/corelib/tools/qvector.h
@@ -523,7 +523,7 @@ QVector<T>::QVector(int asize, const T &t)
d = Data::allocate(asize);
Q_CHECK_PTR(d);
d->size = asize;
- T* i = d->end();
+ auto i = d->end();
while (i != d->begin())
new (--i) T(t);
} else {
@@ -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;