[Development] QProperty and library coding guide

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Fri Jul 17 15:13:52 CEST 2020


Il 17/07/20 11:36, Lars Knoll ha scritto:
> And it’s cleaner, because using the union trick, we do access several members of the union at the same time. It works on all compilers but I’m not 100% convinced it’s fully defined behavior according to C++, even if the members don’t have data.

To me, it's mostly the pointer arithmetic being sketchy (although 
probably legal).

I don't see a particular problem with the union trick -- given N empty 
classes

   class Ex {};

And then the union

   union U { E1 e1; E2 e2; ~~~ EN en; };

Then:

1) All of Ex and therefore U are standard layout classes [class.prop]
2) Therefore, the address of U is the same address as E1 [class.mem§26]
3) All non static data members of U have the same address [class.union]

It follows that U and all of its inner EN subobjects have the same 
address, so you can apply the same offset to all of them reach the 
QObject/gadget that contains U as subobject.


> And why wouldn’t we do it, if the compilers support it?

Because it breaks ABI, and only relatively recent GCC versions have 
support for the attribute... or does GCC have a nonstandard one?

Thanks,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/development/attachments/20200717/65be76d3/attachment-0001.bin>


More information about the Development mailing list