[Development] QProperty and library coding guide

Thiago Macieira thiago.macieira at intel.com
Sun Jul 19 20:42:46 CEST 2020

On Sunday, 19 July 2020 08:20:01 PDT Thiago Macieira wrote:
> 1. Revert the feature.
> 2. Write papers to add necessary functionality to C++23, like reversing a
>    pointer-to-member-object back to the containing object
> 3. Require C++23 in Qt 7.0
> double Square::_qt_property_api_width::value() const
> {
>     return retrieveContainer<&Square::width>(this)->d->width;
> }

I've dug up my old idea of pointer-to-container. This can be implemented as a 
standard library feature, without a core language change. 

Here's a test implementation modernised with C++17 NTTPs:
I *believe* it has no UB but does rely on implementation-defined behaviour of 
a PMO. This IB works for QObject because we don't allow virtual inheritance 
and also because by construction the type is only done on the class that 
introduced the member. A generic solution of PMO reversal requires more work.

In that sense, Peppe's suggestion of C++17 offsetof is better.

The important detail is here:
    static Klass *memberToContainer(Type pmo, ConstMemberType *member)
        quintptr memberAddress = quintptr(member);
        typename QIntegerForSizeof<Type>::Signed offset;
        memcpy(&offset, &pmo, sizeof(offset));
        quintptr containerAddress = memberAddress - offset;
        return reinterpret_cast<Klass *>(containerAddress);

The memcpy is IB. The containerAddress calculation is the same as in the code 
currently generated by moc, which I initially claimed to be UB, but now I'm 
not sure.

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products

More information about the Development mailing list