summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-05-11 14:17:17 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-05-17 06:44:45 +0200
commit8712e35aec2c072fe709364eaa69bb8928c5cacc (patch)
tree6c0a54722d4f784ae279bb07f28fae225684384d
parent782ccc6de5950ff1f6d3eeaaeacc7af4bc97a84f (diff)
downloadqtbase-8712e35aec2c072fe709364eaa69bb8928c5cacc.tar.gz
QVarLengthArray/QList: make assign() return a reference to *this
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 <marten.nordheim@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r--src/corelib/tools/qlist.h12
-rw-r--r--src/corelib/tools/qlist.qdoc6
-rw-r--r--src/corelib/tools/qvarlengtharray.h12
-rw-r--r--src/corelib/tools/qvarlengtharray.qdoc6
-rw-r--r--tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp21
5 files changed, 35 insertions, 22 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h
index 5f5dad583e..79de8fced0 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 <typename InputIterator, if_input_iterator<InputIterator> = 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<T> l)
- { assign(l.begin(), l.end()); }
+ QList &assign(std::initializer_list<T> l)
+ { return assign(l.begin(), l.end()); }
template <typename ...Args>
iterator emplace(const_iterator before, Args&&... args)
diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc
index a23e7d73fb..554d07ec1b 100644
--- a/src/corelib/tools/qlist.qdoc
+++ b/src/corelib/tools/qlist.qdoc
@@ -1538,7 +1538,7 @@
\sa erase
*/
-/*! \fn template <typename T> void QList<T>::assign(qsizetype n, parameter_type t)
+/*! \fn template <typename T> QList<T>::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 <typename T> template <typename InputIterator, if_input_iterator<InputIterator>> void QList<T>::assign(InputIterator first, InputIterator last)
+/*! \fn template <typename T> template <typename InputIterator, if_input_iterator<InputIterator>> QList<T>::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 <typename T> void QList<T>::assign(std::initializer_list<T> l)
+/*! \fn template <typename T> QList<T>::assign(std::initializer_list<T> 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 b91825af80..1759fba415 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 <typename InputIterator, if_input_iterator<InputIterator> = true>
- void assign(InputIterator first, InputIterator last)
- { Base::assign_impl(Prealloc, this->array, first, last); }
- void assign(std::initializer_list<T> 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<T> 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 2e727c5d4a..70dc41685c 100644
--- a/src/corelib/tools/qvarlengtharray.qdoc
+++ b/src/corelib/tools/qvarlengtharray.qdoc
@@ -997,7 +997,7 @@
\sa erase()
*/
-/*! \fn template <class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::assign(qsizetype n, const T &t)
+/*! \fn template <class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::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 <class T, qsizetype Prealloc> template <typename InputIterator, if_input_iterator<InputIterator>> void QVarLengthArray<T, Prealloc>::assign(InputIterator first, InputIterator last)
+/*! \fn template <class T, qsizetype Prealloc> template <typename InputIterator, if_input_iterator<InputIterator>> QVarLengthArray<T, Prealloc>::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 <class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::assign(std::initializer_list<T> list)
+/*! \fn template <class T, qsizetype Prealloc> QVarLengthArray<T, Prealloc>::assign(std::initializer_list<T> 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 24f5dba17e..4c6c0e0671 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<decltype( __VA_ARGS__ )>) { \
+ /* 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<Container>(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<Container>(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<V>{ss}, std::istream_iterator<V>{});
+ RET_CHECK(c.assign(std::istream_iterator<V>{ss}, std::istream_iterator<V>{}));
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<Container>(4);
const S oldCapacity = c.capacity();
std::initializer_list<V> 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
}