[Qt-interest] Placement new in Qt

Brad Howes howes at ll.mit.edu
Wed Jul 1 19:10:32 CEST 2009


On Jul 1, 2009, at 12:49 PM, Eric Clark wrote:

> else if (QTypeInfo<T>::isComplex) new (n) T(t);
>
> What does Qt expect it to do? Does it expect realloc to get called,  
> or does it just expect "n" to be returned?


This should just use the address of 'n' for storage, and initializing  
the new T object as a copy of 't'. No reallocations are possible here.  
Somewhere else, Qt must have made sure that 'n' points to an object  
that is large enough to hold T. The 'isComplex' check (I assume) is to  
differentiate between POD types like int or double and non-POD types,  
since the final clause

> else *reinterpret_cast<T*>(n) = t;


is just doing a (fast) bit-copy from the object to copy 't' to the  
memory pointed to by 't' which is only safe for PODs.

Brad

-- 
Brad Howes
Group 42
MIT Lincoln Laboratory • 244 Wood St. • Lexington, MA 02173
Phone: 781.981.5292 • Fax: 781.981.3495 • Secretary: 781.981.7420




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090701/9ef4ea65/attachment.html 


More information about the Qt-interest-old mailing list