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.dang...@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
smime.p7s
Description: Firma crittografica S/MIME
_______________________________________________ Development mailing list Development@qt-project.org https://lists.qt-project.org/listinfo/development