diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 6a99602153a..06a4b66125f 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -108,11 +108,8 @@ public: inline ~QVarLengthArray() { - if (QTypeInfo::isComplex) { - T *i = ptr + s; - while (i-- != ptr) - i->~T(); - } + if constexpr (QTypeInfo::isComplex) + std::destroy_n(ptr, s); if (ptr != reinterpret_cast(array)) free(ptr); } @@ -157,7 +154,7 @@ public: inline void removeLast() { Q_ASSERT(s > 0); - if (QTypeInfo::isComplex) + if constexpr (QTypeInfo::isComplex) ptr[s - 1].~T(); --s; } @@ -473,14 +470,12 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::append(const T *abuf, qs if (asize >= a) reallocate(s, qMax(s * 2, asize)); - if (QTypeInfo::isComplex) { - // call constructor for new objects (which can throw) - while (s < asize) - new (ptr+(s++)) T(*abuf++); - } else { + if constexpr (QTypeInfo::isComplex) + std::uninitialized_copy_n(abuf, increment, ptr + s); + else memcpy(static_cast(&ptr[s]), static_cast(abuf), increment * sizeof(T)); - s = asize; - } + + s = asize; } template @@ -531,10 +526,10 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::reallocate(qsizetype asi } s = copySize; - if (QTypeInfo::isComplex) { - // destroy remaining old objects - while (osize > asize) - (oldPtr+(--osize))->~T(); + // destroy remaining old objects + if constexpr (QTypeInfo::isComplex) { + if (osize > asize) + std::destroy(oldPtr + asize, oldPtr + osize); } if (oldPtr != reinterpret_cast(array) && oldPtr != ptr) @@ -664,14 +659,10 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray::iterator QVarLengthA qsizetype f = qsizetype(abegin - ptr); qsizetype l = qsizetype(aend - ptr); qsizetype n = l - f; - if (QTypeInfo::isComplex) { - std::copy(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f)); - T *i = ptr + s; - T *b = ptr + s - n; - while (i != b) { - --i; - i->~T(); - } + + if constexpr (QTypeInfo::isComplex) { + std::move(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f)); + std::destroy(ptr + s - n, ptr + s); } else { memmove(static_cast(ptr + f), static_cast(ptr + l), (s - l) * sizeof(T)); }