QVarLengthArray: C++11-ify insert/erase signatures
authorMarc Mutz <marc.mutz@kdab.com>
Thu, 24 May 2012 06:26:46 +0000 (08:26 +0200)
committerQt by Nokia <qt-info@nokia.com>
Thu, 30 Aug 2012 00:28:51 +0000 (02:28 +0200)
In C++11, container insert and erase operations take const_iterators
instead of iterators. This is a bug fix compared to C++98, where the
mere lookup step of a lookup-or-insert operation had to be done using
(mutable) iterators, which is particularly worrisome for Qt containers
that are implicitly shared, because of the unneeded detach in the positive
case.

QVarLengthArray is not implicitly shared, but for consistency, the signatures
should be changed here, too. The reason this commit contains only the change
to QVarLengthArray is that this is by far the easiest container. The
implictly shared containers are harder, because detaching invalidates other
iterators (more than the sister STL container would).

Change-Id: Ib3d98360bfe376b782b9d1283c5fa3555e8a719e
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/tools/qvarlengtharray.h
src/corelib/tools/qvarlengtharray.qdoc

index 58b26be..ba24541 100644 (file)
@@ -167,10 +167,10 @@ public:
     inline const_iterator end() const { return ptr + s; }
     inline const_iterator cend() const { return ptr + s; }
     inline const_iterator constEnd() const { return ptr + s; }
-    iterator insert(iterator before, int n, const T &x);
-    inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
-    iterator erase(iterator begin, iterator end);
-    inline iterator erase(iterator pos) { return erase(pos, pos+1); }
+    iterator insert(const_iterator before, int n, const T &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); }
 
 private:
     friend class QPodList<T, Prealloc>;
@@ -338,7 +338,7 @@ inline void QVarLengthArray<T, Prealloc>::replace(int i, const T &t)
 
 
 template <class T, int Prealloc>
-Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(iterator before, size_type n, const T &t)
+Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(const_iterator before, size_type n, const T &t)
 {
     int offset = int(before - ptr);
     if (n != 0) {
@@ -365,7 +365,7 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthA
 }
 
 template <class T, int Prealloc>
-Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(iterator abegin, iterator aend)
+Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(const_iterator abegin, const_iterator aend)
 {
     int f = int(abegin - ptr);
     int l = int(aend - ptr);
index fbd984e..45eab98 100644 (file)
     \sa constBegin(), end()
 */
 
-/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(iterator pos)
+/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(const_iterator pos)
     \since 4.8
 
     Removes the item pointed to by the iterator \a pos from the
     \sa insert(), remove()
 */
 
-/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(iterator begin, iterator end)
+/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(const_iterator begin, const_iterator end)
 
     \overload
     \since 4.8
     vector.
 */
 
-/*! \fn QVarLengthArray::iterator QVarLengthArray::insert(iterator before, const T &value)
+/*! \fn QVarLengthArray::iterator QVarLengthArray::insert(const_iterator before, const T &value)
 
     \overload
     \since 4.8
     \a before. Returns an iterator pointing at the inserted item.
 */
 
-/*! \fn QVarLengthArray::iterator QVarLengthArray::insert(iterator before, int count, const T &value)
+/*! \fn QVarLengthArray::iterator QVarLengthArray::insert(const_iterator before, int count, const T &value)
 
     \since 4.8
     Inserts \a count copies of \a value in front of the item pointed to