[Qt-interest] QList: Force a deep copy?

Samuel Rødal sroedal at trolltech.com
Thu Jul 2 08:52:51 CEST 2009


Eric Clark wrote:
> I don't typically compile Qt. Usually, I use the binaries that have already been compiled. It may come to me having to do that though. I did notice in the QList class that they are use the placement new operator. Does anyone know what Qt expects this to do?
> 
> template <typename T>
> Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
> {
>     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
>     else if (QTypeInfo<T>::isComplex) new (n) T(t);
>     else *reinterpret_cast<T*>(n) = t;
> }
> 
> In the second line of the function:
> 
> else if (QTypeInfo<T>::isComplex) new (n) T(t);
> 
> Qt is using the placement new operator here. We do not overload this operator in our stuff. What does Qt expect it to do? Does it expect realloc to get called, or does it just expect "n" to be returned?
> 
> Thanks,
> Eric

Placement new simply initializes an instance of T in the given memory 
area, calling the constructor in the process. No new memory is 
allocated. See http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10

Also, when placement new is used delete isn't called, but the destructor 
is called explicitly, like this in QList's case:

reinterpret_cast<T*>(n)->~T()

So it doesn't sound like placement new should cause any problems in 
combination with nedmalloc, since it doesn't do any memory allocation by 
itself.

--
Samuel



More information about the Qt-interest-old mailing list