[Development] QProperty and library coding guide

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Sat Jul 18 16:11:51 CEST 2020


Il 17/07/20 19:55, Thiago Macieira ha scritto:
> moc generates:
> 
> Type Klass::_qt_property_api_propertyName::value() const
> {
>      const size_t propertyMemberOffset =
>       reinterpret_cast<size_t>(&(static_cast<Klass *>(nullptr)->propertyName));
>      const auto *thisPtr = reinterpret_cast<const Klass *>(
>          reinterpret_cast<const char *>(this) - propertyMemberOffset);
>      return thisPtr->d_func()->property.value();
> }
> 
> The first two lines of this function are UB.
> 
> It MUST be fixed.

The first line smells like offsetof, which we might be lucky enough to 
use -- in C++17 it became conditionally supported whether it works on 
non-standard layout classes (QObject), and apparently GCC/Clang/MSVC all 
support it.

The second line smells 99.99% unfixable UB. Would using ASM be an 
acceptable "fix"?

> 
> In the process, please also fix this code:
> 
>      auto prop = object->propertyName;
>      return prop.value();

How? Just like QStringBuilder, there's no way to block a `const auto &`, 
is there?

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/20200718/fd1f88c0/attachment.bin>


More information about the Development mailing list