diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 07e5970faf..4bb9f1a028 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -162,13 +162,19 @@ public: inline iterator operator++(int) { T *n = i; ++i; return n; } inline iterator &operator--() { i--; return *this; } inline iterator operator--(int) { T *n = i; i--; return n; } - inline iterator &operator+=(qsizetype j) { i+=j; return *this; } - inline iterator &operator-=(qsizetype j) { i-=j; return *this; } - inline iterator operator+(qsizetype j) const { return iterator(i+j); } - inline iterator operator-(qsizetype j) const { return iterator(i-j); } - friend inline iterator operator+(qsizetype j, iterator k) { return k + j; } inline qsizetype operator-(iterator j) const { return i - j.i; } inline operator T*() const { return i; } + + template std::enable_if_t, iterator> + &operator+=(Int j) { i+=j; return *this; } + template std::enable_if_t, iterator> + &operator-=(Int j) { i-=j; return *this; } + template std::enable_if_t, iterator> + operator+(Int j) const { return iterator(i+j); } + template std::enable_if_t, iterator> + operator-(Int j) const { return iterator(i-j); } + template friend std::enable_if_t, iterator> + operator+(Int j, iterator k) { return k + j; } }; class const_iterator { @@ -206,13 +212,19 @@ public: inline const_iterator operator++(int) { const T *n = i; ++i; return n; } inline const_iterator &operator--() { i--; return *this; } inline const_iterator operator--(int) { const T *n = i; i--; return n; } - inline const_iterator &operator+=(qsizetype j) { i+=j; return *this; } - inline const_iterator &operator-=(qsizetype j) { i-=j; return *this; } - inline const_iterator operator+(qsizetype j) const { return const_iterator(i+j); } - inline const_iterator operator-(qsizetype j) const { return const_iterator(i-j); } - friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; } inline qsizetype operator-(const_iterator j) const { return i - j.i; } inline operator const T*() const { return i; } + + template std::enable_if_t, const_iterator> + &operator+=(Int j) { i+=j; return *this; } + template std::enable_if_t, const_iterator> + &operator-=(Int j) { i-=j; return *this; } + template std::enable_if_t, const_iterator> + operator+(Int j) const { return const_iterator(i+j); } + template std::enable_if_t, const_iterator> + operator-(Int j) const { return const_iterator(i-j); } + template friend std::enable_if_t, const_iterator> + operator+(Int j, const_iterator k) { return k + j; } }; using Iterator = iterator; using ConstIterator = const_iterator; diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 3d26720fab..78a6ca317a 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -2404,30 +2404,97 @@ void tst_QList::iterators() const idx = 0; auto it = v.begin(); QCOMPARE(*it, idx); + // idx == 0 std::advance(it, 7); idx += 7; QCOMPARE(*it, idx); + // idx == 7 it++; idx++; QCOMPARE(*it, idx); + // idx == 8 ++it; ++idx; QCOMPARE(*it, idx); + // idx == 9 std::advance(it, -3); idx -= 3; QCOMPARE(*it, idx); + // idx == 6 it--; idx--; QCOMPARE(*it, idx); + // idx == 5 --it; --idx; QCOMPARE(*it, idx); + // idx == 4 + + it = it + 1; + idx = idx + 1; + QCOMPARE(*it, idx); + // idx == 5 + + it = it + ptrdiff_t(1); + idx = idx + 1; + QCOMPARE(*it, idx); + // idx == 6 + + it = it + qsizetype(1); + idx = idx + 1; + QCOMPARE(*it, idx); + // idx == 7 + + it = it - qsizetype(1); + idx = idx - 1; + QCOMPARE(*it, idx); + // idx == 6 + + it = it - ptrdiff_t(1); + idx = idx - 1; + QCOMPARE(*it, idx); + // idx == 5 + + it = it - 1; + idx = idx - 1; + QCOMPARE(*it, idx); + // idx == 4 + + it -= 1; + idx -= 1; + QCOMPARE(*it, idx); + // idx == 3 + + it -= qsizetype(1); + idx -= 1; + QCOMPARE(*it, idx); + // idx == 2 + + it -= ptrdiff_t(1); + idx -= 1; + QCOMPARE(*it, idx); + // idx == 1 + + it += ptrdiff_t(1); + idx += 1; + QCOMPARE(*it, idx); + // idx == 2 + + it += qsizetype(1); + idx += 1; + QCOMPARE(*it, idx); + // idx == 3 + + it += 1; + idx += 1; + QCOMPARE(*it, idx); + // idx == 4 *it = idx + 1; QCOMPARE(*it, idx + 1); @@ -2484,30 +2551,97 @@ void tst_QList::constIterators() const qsizetype idx = 0; auto it = v.cbegin(); QCOMPARE(*it, idx); + // idx == 0 std::advance(it, 7); idx += 7; QCOMPARE(*it, idx); + // idx == 7 it++; idx++; QCOMPARE(*it, idx); + // idx == 8 ++it; ++idx; QCOMPARE(*it, idx); + // idx == 9 std::advance(it, -3); idx -= 3; QCOMPARE(*it, idx); + // idx == 6 it--; idx--; QCOMPARE(*it, idx); + // idx == 5 --it; --idx; QCOMPARE(*it, idx); + // idx == 4 + + it = it + 1; + idx = idx + 1; + QCOMPARE(*it, idx); + // idx == 5 + + it = it + ptrdiff_t(1); + idx = idx + 1; + QCOMPARE(*it, idx); + // idx == 6 + + it = it + qsizetype(1); + idx = idx + 1; + QCOMPARE(*it, idx); + // idx == 7 + + it = it - qsizetype(1); + idx = idx - 1; + QCOMPARE(*it, idx); + // idx == 6 + + it = it - ptrdiff_t(1); + idx = idx - 1; + QCOMPARE(*it, idx); + // idx == 5 + + it = it - 1; + idx = idx - 1; + QCOMPARE(*it, idx); + // idx == 4 + + it -= 1; + idx -= 1; + QCOMPARE(*it, idx); + // idx == 3 + + it -= qsizetype(1); + idx -= 1; + QCOMPARE(*it, idx); + // idx == 2 + + it -= ptrdiff_t(1); + idx -= 1; + QCOMPARE(*it, idx); + // idx == 1 + + it += ptrdiff_t(1); + idx += 1; + QCOMPARE(*it, idx); + // idx == 2 + + it += qsizetype(1); + idx += 1; + QCOMPARE(*it, idx); + // idx == 3 + + it += 1; + idx += 1; + QCOMPARE(*it, idx); + // idx == 4 // stl-style reverse iterators idx = v.size() - 1;