Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-93392

QVector in Qt5 still requires default constructible types

    XMLWordPrintable

    Details

    • Commits:
      747800675044bc1782b9bc9b97cea2cc2ef2c06f (qt/tqtc-qtbase/5.15)

      Description

      QVector::insert(iter, N, value) goes through default construction and then shuffling objects around. But for such an operation there is no need of default construction; and QVector is now supposed to support non-default constructible types.

      Annotated code:

      template <typename T>
      typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
      {
          Q_ASSERT_X(isValidIterator(before),  "QVector::insert", "The specified iterator argument 'before' is invalid");
      
          const auto offset = std::distance(d->begin(), before);
          if (n != 0) {
              const T copy(t);
              if (!isDetached() || d->size + n > int(d->alloc))
                  realloc(d->size + n, QArrayData::Grow);
              if (!QTypeInfoQuery<T>::isRelocatable) {
                  T *b = d->end();
                  T *i = d->end() + n;
                  while (i != b)
                      new (--i) T; // <-- the problem right here
                  i = d->end();
                  T *j = i + n;
                  b = d->begin() + offset;
                  while (i != b)
                      *--j = *--i; // ???
                  i = b+n;
                  while (i != b)
                      *--i = copy;
              } else {
                  T *b = d->begin() + offset;
                  T *i = b + n;
                  memmove(static_cast<void *>(i), static_cast<const void *>(b), (d->size - offset) * sizeof(T));
                  while (i != b)
                      new (--i) T(copy);
              }
              d->size += n;
          }
          return d->begin() + offset;
      }
      

      The code is also smelly; you move objects to make room, you don't copy them...

      I'd patch it myself, but I can't push the patch anywhere.

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            eugmes Ievgenii Meshcheriakov
            Reporter:
            peppe Giuseppe D'Angelo
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes