From 29cfea3e82e04345b2cbb07c3ebebebbe3ced0bb Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 12 Aug 2021 12:13:15 +0200 Subject: QVarLengthArray: add support for emplacement Take the rvalue insert() function and turn it into the emplace() function. Reformulate rvalue-insert using emplace(). Lvalue insert() is using a different code path, so leave that alone. This way, we don't need to go overboard with testing. [ChangeLog][QtCore][QVarLengthArray] Added emplace(), emplace_back(). Change-Id: I3e1400820ae0dd1fe87fd4b4c518f7f40be39f8b Reviewed-by: Thiago Macieira Reviewed-by: Edward Welbourne --- src/corelib/tools/qvarlengtharray.h | 16 +++++++++++----- src/corelib/tools/qvarlengtharray.qdoc | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 24505d629b..3aa5e70114 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -288,7 +288,7 @@ public: const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } iterator insert(const_iterator before, qsizetype n, const T &x); - iterator insert(const_iterator before, T &&x); + iterator insert(const_iterator before, T &&x) { return emplace(before, std::move(x)); } inline iterator insert(const_iterator before, const T &x) { return insert(before, 1, x); } iterator erase(const_iterator begin, const_iterator end); inline iterator erase(const_iterator pos) { return erase(pos, pos + 1); } @@ -303,6 +303,11 @@ public: inline T &back() { return last(); } inline const T &back() const { return last(); } void shrink_to_fit() { squeeze(); } + template + iterator emplace(const_iterator pos, Args &&...args); + template + T &emplace_back(Args &&...args) { return *emplace(cend(), std::forward(args)...); } + #ifdef Q_QDOC template @@ -612,7 +617,8 @@ inline void QVarLengthArray::replace(qsizetype i, const T &t) } template -Q_OUTOFLINE_TEMPLATE typename QVarLengthArray::iterator QVarLengthArray::insert(const_iterator before, T &&t) +template +Q_OUTOFLINE_TEMPLATE auto QVarLengthArray::emplace(const_iterator before, Args &&...args) -> iterator { Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid"); @@ -627,14 +633,14 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray::iterator QVarLengthA new (--j) T(std::move(*--i)); while (i != b) *--j = std::move(*--i); - *b = std::move(t); + *b = T(std::forward(args)...); } else { - new (b) T(std::move(t)); + new (b) T(std::forward(args)...); } } else { T *b = ptr + offset; memmove(static_cast(b + 1), static_cast(b), (s - offset) * sizeof(T)); - new (b) T(std::move(t)); + new (b) T(std::forward(args)...); } s += 1; return ptr + offset; diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index 52261de4c6..485b531805 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -753,6 +753,28 @@ \a before. Returns an iterator pointing at the inserted item. */ +/*! + \fn template QVarLengthArray::emplace(const_iterator before, Args &&...args); + + \since 6.3 + + Inserts an item in front of the item pointed to by the iterator + \a before, passing \a args to its constructor. + + Returns an iterator pointing at the emplaced item. +*/ + +/*! + \fn template QVarLengthArray::emplace_back(Args &&...args); + + \since 6.3 + + Inserts an item at the back of this QVarLengthArray, passing + \a args to its constructor. + + Returns a reference to the emplaced item. +*/ + /*! \fn template QVarLengthArray::iterator QVarLengthArray::insert(const_iterator before, qsizetype count, const T &value) \since 4.8 -- cgit v1.2.3