From 6e8985e3576a4439bd66c0767f9912d1e124682c Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Wed, 2 Sep 2020 15:19:16 +0200 Subject: Make Q*ArrayOps erase aligned with std::vector::erase Scoped GrowsBackwards-optimized erase to only be applied when erase starts at the beginning of the element range. In other cases, old "left-shifting" erase is used to align with std::vector::erase invalidation policy Task-number: QTBUG-84320 Change-Id: I2e7f3b96b056bc371119eb2d36cc7c74af52c394 Reviewed-by: Thiago Macieira --- src/corelib/tools/qarraydataops.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/corelib/tools/qarraydataops.h') diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index db795e24135..0230677330c 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -1497,11 +1497,11 @@ public: Q_ASSERT(b >= this->begin() && b < this->end()); Q_ASSERT(e > this->begin() && e <= this->end()); - // Qt5 QList in erase: try to move less data around - // Now: - const T *begin = this->begin(); - const T *end = this->end(); - if (b - begin < end - e) { + // Comply with std::vector::erase(): erased elements and all after them + // are invalidated. However, erasing from the beginning effectively + // means that all iterators are invalidated. We can use this freedom to + // erase by moving towards the end. + if (b == this->begin()) { Base::erase(GrowsBackwardsTag{}, b, e); } else { Base::erase(GrowsForwardTag{}, b, e); -- cgit v1.2.3