diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index c2414f22f2..a0c3da7cce 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -1373,18 +1373,32 @@ public: template void emplaceBack(Args&&... args) { - Q_ASSERT(!this->isShared()); - Q_ASSERT(this->freeSpaceAtEnd() >= 1); - new (this->end()) T(std::forward(args)...); + if (this->needsDetach() || !this->freeSpaceAtEnd()) { + // protect against args being an element of the container + T tmp(std::forward(args)...); + this->reallocateAndGrow(QArrayData::GrowsAtEnd, 1); + Q_ASSERT(!this->isShared()); + Q_ASSERT(this->freeSpaceAtEnd() >= 1); + new (this->end()) T(std::move(tmp)); + } else { + new (this->end()) T(std::forward(args)...); + } ++this->size; } template void emplaceFront(Args&&... args) { - Q_ASSERT(!this->isShared()); - Q_ASSERT(this->freeSpaceAtBegin() >= 1); - new (this->ptr - 1) T(std::forward(args)...); + if (this->needsDetach() || !this->freeSpaceAtBegin()) { + // protect against args being an element of the container + T tmp(std::forward(args)...); + this->reallocateAndGrow(QArrayData::GrowsAtBeginning, 1); + Q_ASSERT(!this->isShared()); + Q_ASSERT(this->freeSpaceAtBegin() >= 1); + new (this->ptr - 1) T(std::move(tmp)); + } else { + new (this->ptr - 1) T(std::forward(args)...); + } --this->ptr; ++this->size; } diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 9c95d49bae..eefe7b448f 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -715,14 +715,7 @@ template template inline typename QList::reference QList::emplaceFront(Args &&... args) { - if (d->needsDetach() || !d.freeSpaceAtBegin()) { - // protect against args being an element of the container - T tmp(std::forward(args)...); - d.reallocateAndGrow(QArrayData::GrowsAtBeginning, 1); - d->emplaceFront(std::move(tmp)); - } else { - d->emplaceFront(std::forward(args)...); - } + d->emplaceFront(std::forward(args)...); return *d.begin(); } @@ -751,14 +744,7 @@ template template inline typename QList::reference QList::emplaceBack(Args &&... args) { - if (d->needsDetach() || !d.freeSpaceAtEnd()) { - // protect against args being an element of the container - T tmp(std::forward(args)...); - d.reallocateAndGrow(QArrayData::GrowsAtEnd, 1); - d->emplaceBack(std::move(tmp)); - } else { - d->emplaceBack(std::forward(args)...); - } + d->emplaceBack(std::forward(args)...); return *(d.end() - 1); }