From 8712e35aec2c072fe709364eaa69bb8928c5cacc Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 11 May 2023 14:17:17 +0200 Subject: [PATCH] QVarLengthArray/QList: make assign() return a reference to *this MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While std::vector::assign() returns void, std::basic_string::assign() returns std::basic_string&. In Qt, we want to be consistent between {QVLA,QList,QString,QByteArray}::assign(), and returning *this is the more general solution, so do that. Task-number: QTBUG-106196 Task-number: QTBUG-106200 Change-Id: I2689b4af032ab6fb3f8fbcb4d825d5201ea5abeb Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Qt CI Bot --- src/corelib/tools/qlist.h | 12 +++++------ src/corelib/tools/qlist.qdoc | 6 +++--- src/corelib/tools/qvarlengtharray.h | 12 +++++------ src/corelib/tools/qvarlengtharray.qdoc | 6 +++--- .../tst_containerapisymmetry.cpp | 21 +++++++++++++++---- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 5f5dad583e4..79de8fced0b 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -496,18 +496,18 @@ public: } } - void assign(qsizetype n, parameter_type t) + QList &assign(qsizetype n, parameter_type t) { Q_ASSERT(n >= 0); - fill(t, n); + return fill(t, n); } template = true> - void assign(InputIterator first, InputIterator last) - { d.assign(first, last); } + QList &assign(InputIterator first, InputIterator last) + { d.assign(first, last); return *this; } - void assign(std::initializer_list l) - { assign(l.begin(), l.end()); } + QList &assign(std::initializer_list l) + { return assign(l.begin(), l.end()); } template iterator emplace(const_iterator before, Args&&... args) diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc index a23e7d73fbd..554d07ec1b3 100644 --- a/src/corelib/tools/qlist.qdoc +++ b/src/corelib/tools/qlist.qdoc @@ -1538,7 +1538,7 @@ \sa erase */ -/*! \fn template void QList::assign(qsizetype n, parameter_type t) +/*! \fn template QList::assign(qsizetype n, parameter_type t) \since 6.6 Replaces the contents of this list with \a n copies of \a t. @@ -1549,7 +1549,7 @@ list or this list is shared. */ -/*! \fn template template > void QList::assign(InputIterator first, InputIterator last) +/*! \fn template template > QList::assign(InputIterator first, InputIterator last) \since 6.6 Replaces the contents of this list with a copy of the elements in the @@ -1569,7 +1569,7 @@ *this. */ -/*! \fn template void QList::assign(std::initializer_list l) +/*! \fn template QList::assign(std::initializer_list l) \since 6.6 Replaces the contents of this list with a copy of the elements of diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index b91825af806..1759fba4158 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -501,13 +501,13 @@ public: void insert(qsizetype i, const T &t); void insert(qsizetype i, qsizetype n, const T &t); - void assign(qsizetype n, const T &t) - { Base::assign_impl(Prealloc, this->array, n, t); } + QVarLengthArray &assign(qsizetype n, const T &t) + { Base::assign_impl(Prealloc, this->array, n, t); return *this; } template = true> - void assign(InputIterator first, InputIterator last) - { Base::assign_impl(Prealloc, this->array, first, last); } - void assign(std::initializer_list list) - { assign(list.begin(), list.end()); } + QVarLengthArray &assign(InputIterator first, InputIterator last) + { Base::assign_impl(Prealloc, this->array, first, last); return *this; } + QVarLengthArray &assign(std::initializer_list list) + { assign(list.begin(), list.end()); return *this; } #ifdef Q_QDOC void replace(qsizetype i, const T &t); diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index 2e727c5d4a9..70dc41685c4 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -997,7 +997,7 @@ \sa erase() */ -/*! \fn template void QVarLengthArray::assign(qsizetype n, const T &t) +/*! \fn template QVarLengthArray::assign(qsizetype n, const T &t) \since 6.6 Replaces the contents of this container with \a n copies of \a t. @@ -1006,7 +1006,7 @@ allocate memory if \a n exceeds the capacity of the container. */ -/*! \fn template template > void QVarLengthArray::assign(InputIterator first, InputIterator last) +/*! \fn template template > QVarLengthArray::assign(InputIterator first, InputIterator last) \since 6.6 Replaces the contents of this container with a copy of the elements in the @@ -1023,7 +1023,7 @@ The behavior is undefined if either argument is an iterator into *this. */ -/*! \fn template void QVarLengthArray::assign(std::initializer_list list) +/*! \fn template QVarLengthArray::assign(std::initializer_list list) \since 6.6 Replaces the contents of this container with a copy of the elements of \a list. diff --git a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp index 24f5dba17ea..4c6c0e06713 100644 --- a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp +++ b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp @@ -778,6 +778,18 @@ void tst_ContainerApiSymmetry::assign_impl() const for (const auto &e : Arr) \ QCOMPARE(e, ComparisonData) \ /*end*/ +#define RET_CHECK(...) \ + do { \ + if constexpr (std::is_void_v) { \ + /* e.g. std::vector */ \ + __VA_ARGS__ ; \ + } else { \ + /* e.g. std::basic_string */ \ + auto &&r = __VA_ARGS__ ; \ + QCOMPARE_EQ(&r, &c); \ + } \ + } while (false) \ + /* end */ using V = typename Container::value_type; using S = typename Container::size_type; auto tData = V(9); @@ -785,7 +797,7 @@ void tst_ContainerApiSymmetry::assign_impl() const // fill version auto c = make(4); const S oldCapacity = c.capacity(); - c.assign(4, tData); + RET_CHECK(c.assign(4, tData)); CHECK(c, tData, c.size(), S(4), c.capacity(), oldCapacity); c.assign(8, tData); @@ -801,7 +813,7 @@ void tst_ContainerApiSymmetry::assign_impl() const auto iter = make(1); iter.assign(8, tData); - c.assign(iter.begin(), iter.end()); + RET_CHECK(c.assign(iter.begin(), iter.end())); CHECK(c, tData, c.size(), S(8), c.capacity(), std::max(oldCapacity, S(8))); c.assign(iter.begin(), iter.begin()); @@ -814,7 +826,7 @@ void tst_ContainerApiSymmetry::assign_impl() const const S oldCapacity = c.capacity(); std::stringstream ss("9 9 "); - c.assign(std::istream_iterator{ss}, std::istream_iterator{}); + RET_CHECK(c.assign(std::istream_iterator{ss}, std::istream_iterator{})); CHECK(c, tData, c.size(), S(2), c.capacity(), oldCapacity); ss.str(""); @@ -836,10 +848,11 @@ void tst_ContainerApiSymmetry::assign_impl() const auto c = make(4); const S oldCapacity = c.capacity(); std::initializer_list list = {tData, tData, tData}; - c.assign(list); + RET_CHECK(c.assign(list)); CHECK(c, tData, c.size(), S(3), c.capacity(), oldCapacity); } +#undef RET_CHECK #undef CHECK }