[Development] OK to add zero/bulk copy to QVector?

Marc Mutz marc.mutz at kdab.com
Mon Jan 6 11:19:30 CET 2014


On Monday 30 December 2013 02:42:25 Konstantin Ritt wrote:
> >  the 1st one is created automatically when the parent class
> 
> is created which holds the vector, and a second vector is created when
> calling "fromRawData()" when I got the data on the heap to replace
> the vector's buffer.
> 
> >From the one side, looking to the "QVector(const T *data, int size)" API,
> 
> one'd say QVector copies the data so that the original data could be freed
> (because we do that everywhere else, look i.e. QString, QByteArray,
> etc.)...surprise;
> 
> >From the other side, creating a null QVector() (by using a default ctor)
> >is
> 
> no-alloc operation and costs you near to nothing (note Qt containers use
> the power of COW). "QVector<T> data = QVector<T>::fromRawData(dataPtr,
> dataSize)" costs you near to nothing once again but has a pretty nice,
> non-ambiguous "fromRawData" name.

It actually costs nothing.

And if you can't co-locate definition and first assignment, use QVector::swap() 
for fast assignment.

-- 
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions



More information about the Development mailing list