Here is the implementation of QVector's append() from github:
template <typename T>
void QVector<T>::append(const T &t)
{
if (d->ref != 1 || d->size + 1 > d->alloc) {
const T copy(t);
realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T),
QTypeInfo<T>::isStatic));
if (QTypeInfo<T>::isComplex)
new (p->array + d->size) T(copy);
else
p->array[d->size] = copy;
} else {
if (QTypeInfo<T>::isComplex)
new (p->array + d->size) T(t);
else
p->array[d->size] = t;
}
++d->size;
}
Why does it need to make a copy of t
if the number of references to the vector != 1 or it needs to resize and why does it make a copy only for these conditions?
A related question has been asked here, but in the code there a copy of t
is always made before appending to the underlying array.