Details
-
Bug
-
Resolution: Done
-
P2: Important
-
5.15.2, 5.15.6
-
None
-
747800675044bc1782b9bc9b97cea2cc2ef2c06f (qt/tqtc-qtbase/5.15)
-
Team 1 Foundation_Sprint 44
Description
QVector::insert(iter, N, value) goes through default construction and then shuffling objects around. But for such an operation there is no need of default construction; and QVector is now supposed to support non-default constructible types.
Annotated code:
template <typename T> typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t) { Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid"); const auto offset = std::distance(d->begin(), before); if (n != 0) { const T copy(t); 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; // <-- the problem right here i = d->end(); T *j = i + n; b = d->begin() + offset; while (i != b) *--j = *--i; // ??? i = b+n; while (i != b) *--i = copy; } else { T *b = d->begin() + offset; T *i = b + n; memmove(static_cast<void *>(i), static_cast<const void *>(b), (d->size - offset) * sizeof(T)); while (i != b) new (--i) T(copy); } d->size += n; } return d->begin() + offset; }
The code is also smelly; you move objects to make room, you don't copy them...
I'd patch it myself, but I can't push the patch anywhere.