diff --git a/src/corelib/tools/qcontainertools_impl.h b/src/corelib/tools/qcontainertools_impl.h index f63a583d4c..dec60aa076 100644 --- a/src/corelib/tools/qcontainertools_impl.h +++ b/src/corelib/tools/qcontainertools_impl.h @@ -86,6 +86,26 @@ void q_uninitialized_relocate_n(T* first, N n, T* out) QT_WARNING_POP +/*! + \internal + + A wrapper around std::rotate(), with an optimization for + Q_RELOCATABLE_TYPEs. We omit the return value, as it would be more work to + compute in the Q_RELOCATABLE_TYPE case and, unlike std::rotate on + ForwardIterators, callers can compute the result in constant time + themselves. +*/ +template +void q_rotate(T *first, T *mid, T *last) +{ + if constexpr (QTypeInfo::isRelocatable) { + const auto cast = [](T *p) { return reinterpret_cast(p); }; + std::rotate(cast(first), cast(mid), cast(last)); + } else { + std::rotate(first, mid, last); + } +} + /*! \internal Copies all elements, except the ones for which \a pred returns \c true, from diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index dc6e88e3e8..6bd2ecdf4c 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -916,12 +916,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase::emplace_impl(qsizetype prealloc, void *ar emplace_back_impl(prealloc, array, std::forward(args)...); const auto b = begin() + offset; const auto e = end(); - if constexpr (QTypeInfo::isRelocatable) { - auto cast = [](T *p) { return reinterpret_cast(p); }; - std::rotate(cast(b), cast(e - 1), cast(e)); - } else { - std::rotate(b, e - 1, e); - } + QtPrivate::q_rotate(b, e - 1, e); return b; }