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>
This commit is contained in:
Marc Mutz 2023-05-11 14:17:17 +02:00
parent 782ccc6de5
commit 8712e35aec
5 changed files with 35 additions and 22 deletions

View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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.

View File

@ -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
}