Results 1 to 4 of 4

Thread: QVector creates double copies on replace, append, ...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QVector creates double copies on replace, append, ...

    Hi there

    Today I've noticed that all my appends to QVector create double copies of the data. So after inspection of the code I
    found that not just append has the same issue, but replace and others too:

    Source code of replace:
    template <typename T>
    inline void QVector<T>::replace(int i, const T &t)
    {
    Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
    const T copy(t);
    data()[i] = copy;
    }

    Now can somebody please explain me why a local copy is made?

    From my point of view:
    1. we don't need a local copy in the first place
    2. but if we had it, why is it defined as a 'const', so that even a move operation can't be called on the data??

    Now I have to assume that author wanted to do something with this (maybe some compiler issues??), but I can't find the explanation.
    Any insights?

    Best regards
    Waldemar

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector creates double copies on replace, append, ...

    The vector, by definition, holds copies. Whether these are shallow or deep copies, it depends on the data held but they are still copies. The reference operator does not create a copy therefore a copy had to be made explicitly before the assignment operator was used.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector creates double copies on replace, append, ...

    I've done some more inspection and there is no move semantics used on the QVector items.
    I've updated QVector on my local code, but don't understand why this isn't done by default already?


    Added after 5 minutes:


    To wysota:
    This can't be really the reason. The thing is that as soon as we define an argument with a 'const reference' the compiler will automatically call copy constructor/operator even if we use it as:

    template <typename T>
    inline void QVector<T>::replace(int i, const T &t)
    {
    Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
    data()[i] = t;
    }

    Of course I can be wrong... do you have some example in which circumstances a 'const reference' wouldn't call copy constructor/operator?
    Last edited by Waldemar; 30th December 2014 at 22:01.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector creates double copies on replace, append, ...

    Quote Originally Posted by Waldemar View Post
    Of course I can be wrong... do you have some example in which circumstances a 'const reference' wouldn't call copy constructor/operator?
    I never claimed anything like this. Indeed the data is copied twice during append, I don't know why, this is different to std::vector.

    You could try looking here for answers: http://doc.qt.digia.com/qq/qq19-containers.html. The behavior changes if you use Q_DECLARE_TYPEINFO on your class and make it a primitive type.

    Maybe it has something to do with reference counting (to ensure the count doesn't drop between a certain value during assignment)?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QVector.append rounding values
    By splinterz in forum Qt Programming
    Replies: 4
    Last Post: 9th July 2014, 12:08
  2. QFile::copy copies only 3kb
    By camol in forum Qt Programming
    Replies: 36
    Last Post: 20th April 2011, 19:52
  3. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 09:09
  4. QVector - Append
    By krishbhala in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2007, 10:50
  5. How many copies of Signal parameters
    By Doug Broadwell in forum Newbie
    Replies: 3
    Last Post: 2nd December 2006, 21:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.