[Development] QProperty and library coding guide

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Sun Jul 19 23:42:24 CEST 2020


Il 19/07/20 20:42, Thiago Macieira ha scritto:
> In that sense, Peppe's suggestion of C++17 offsetof is better.

To clarify, I was proposing to do something like this:

> Type Klass::_qt_property_api_propertyName::value() const
> {
>     /*1*/ const size_t propertyMemberOffset = reinterpret_cast<size_t>(&(static_cast<Klass *>(nullptr)->propertyName));
>     /*2*/ const auto *thisPtr = reinterpret_cast<const Klass *>(reinterpret_cast<const char *>(this) - propertyMemberOffset);
>     return thisPtr->d_func()->property.value();
> }

* change /*1*/ become offsetof(Klass, propertyName);

* change /*2*/ to use ASM. We know the ABI of the platforms we support 
and we know how to calculate the correct pointer value, we just need to 
stop C/C++ from reason about it and flagging it as possible UB. Yes, 
it's a crude hack... :(

My 2 c,
-- 
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/20200719/02d972f7/attachment.bin>


More information about the Development mailing list