[Development] QMetaType and non-const references

Thiago Macieira thiago.macieira at intel.com
Wed Jul 20 00:41:53 CEST 2022

On Tuesday, 19 July 2022 13:16:15 PDT Volker Hilsheimer wrote:
> Ok, so QMetaType::name() will not necessarily return the name that was used
> in QMetaType::fromName, and the name returned might be compiler and C++
> library specific.
> That is good to know and should perhaps be mentioned in the documentation.
> It means that QMetaType objects cannot be serialized, I suppose.

Right. That was the whole thing about meta objects storing the meta types of 
the types in question, so we don't have to compare strings any more. The only 
problem is what happens if your method takes the type in question by const-ref 
and the type is only forward-declared at that point. For example:

   void myslot(const std::string &s);

However, for std::string and the majority of Standard Library types, you're 
not allowed to forward-declare them (I know about <iosfwd>, but stop using 
iostreams, please). You *have* to #include <string> to get std::string defined. 
In fact, this is what allows std::__cxx11::basic_string  and 
std::__1::basic_string to exist in the first place. Therefore, if you do have 
that method above, QMetaObject will record the full metatype for std::string, 
which includes function pointers to create it, copy it, destroy it, etc.

So when you do a signal-slot connection, both signal and slot will know this 
meta type. After my changes, you'll also implicitly pass the meta type to 
QMetaObject::invokeMethod(), QMetaMethod::invoke() or 
QMetaObject::newInstance(), so again the comparison will hold true and the 
invocation happens.

None of this requires Q_DECLARE_METATYPE or qRegisterMetaType.

Here's what a meta type interface looks like:
        .short  0                               # 0x0
        .short  8                               # 0x8
        .long   32                              # 0x20
        .long   49155                           # 0xc003
        .zero   4
        .quad   0
        .quad   QtPrivate::....::name
        .quad   QtPrivate::.....:getDefaultCtr()::{lambda}
        .quad   QtPrivate::.....::getCopyCtr()::{lambda}
        .quad   QtPrivate::....::getMoveCtr()::{lambda}
        .quad   QtPrivate::....::getDtor()::{lambda}
        .quad   QtPrivate::....::equals
        .quad   QtPrivate::....::lessThan
        .quad   0
        .quad   0
        .quad   0
        .quad   0

Thiago Macieira - thiago.macieira (AT) intel.com
  Cloud Software Architect - Intel DCAI Cloud Engineering

More information about the Development mailing list