Hi all!
While I was looking at the code of qvector.h (the header file of QVector class - I use Qt 4.2.2) and I noticed something which I don't understand. For example, here is the implementation of the QVector:append() method:

Qt Code:
  1. template <typename T>
  2. void QVector<T>::append(const T &t)
  3. {
  4. const T copy(t);
  5. if (d->ref != 1 || d->size + 1 > d->alloc)
  6. realloc(d->size, QVectorData::grow(sizeof(Data), d->size + 1, sizeof(T),
  7. QTypeInfo<T>::isStatic));
  8. if (QTypeInfo<T>::isComplex)
  9. new (d->array + d->size) T(copy);
  10. else
  11. d->array[d->size] = copy;
  12. ++d->size;
  13. }
To copy to clipboard, switch view to plain text mode 

So my question is:
What's the use of the copy variable? Is it there for a reason or is it redundant?

Would the following code:

Qt Code:
  1. template <typename T>
  2. void QVector<T>::append(const T &t)
  3. {
  4. // const T copy(t);
  5. if (d->ref != 1 || d->size + 1 > d->alloc)
  6. realloc(d->size, QVectorData::grow(sizeof(Data), d->size + 1, sizeof(T),
  7. QTypeInfo<T>::isStatic));
  8. if (QTypeInfo<T>::isComplex)
  9. new (d->array + d->size) T(t);
  10. else
  11. d->array[d->size] = t;
  12. ++d->size;
  13. }
To copy to clipboard, switch view to plain text mode 

do the same thing as the original code?

The same practice seems to be used in other functions of QVector as well like in QVector::fill():

Qt Code:
  1. template <typename T>
  2. QVector<T> &QVector<T>::fill(const T &from, int asize)
  3. {
  4. const T copy(from); //is it really needed???
  5. resize(asize < 0 ? d->size : asize);
  6. if (d->size) {
  7. T *i = d->array + d->size;
  8. T *b = d->array;
  9. while (i != b)
  10. *--i = copy;
  11. }
  12. return *this;
  13. }
To copy to clipboard, switch view to plain text mode 

and probably in other classes too.

So, is this practice of creating a const copy (as an automatic variable) redundant or am I missing something???