[Interest] Is it safe to call qRegisterMetaType() before main()?

Nikos Chantziaras realnc at gmail.com
Mon Jul 29 20:58:09 CEST 2019


The linker might remove it from the executable when linking statically, 
because 'registerHelper' is not referenced anywhere.


On 29/07/2019 19:25, Tomasz Olszak wrote:
> What's is the cons of putting something like that in anonymous namespace 
> instead of constructor?
> 
> namespace {
> struct RegisterHelper
> {
>      RegisterHelper()
>      {
>          qRegisterMetaType<MyClass>();
>          qRegisterMetaType<MyEnum>();
>          qRegisterMetaTypeStreamOperators<MyClass>();
>      }
> };
> static RegisterHelper registerHelper;
> }
> 
> 
> niedz., 28 lip 2019 o 06:22 Thiago Macieira <thiago.macieira at intel.com 
> <mailto:thiago.macieira at intel.com>> napisał(a):
> 
>     On Friday, 26 July 2019 17:35:03 PDT Nikos Chantziaras wrote:
>      > I didn't (although I use Q_ENUM, but it does the same job.) But
>     there's
>      > no constructor to put qRegisterMetaType() in. It's an enum.
>     There's no
>      > constructor. Some enums are in a namespace, some are in a class, but
>      > even with an enum that is in a class, users of can just use the
>     enum in
>      > their own signals/slots prior to instantiating the class that
>     declares
>      > the enum. It will stay unregistered until the first instance of the
>      > class is created.
>      > ____________________
> 
>     I meant in the class it's most likely to be used in.
> 
>     This is only required when referring to it by name, such as in
>     old-style
>     signal-slot connection, QML, D-Bus, etc.
> 
>     Loading it into a QVariant automatically registers, as QVariant calls
>     qMetaTypeId<Enum>() to get the metatype ID.
> 
>     -- 
>     Thiago Macieira - thiago.macieira (AT) intel.com <http://intel.com>
>        Software Architect - Intel System Software Products
> 
> 
> 
>     _______________________________________________
>     Interest mailing list
>     Interest at qt-project.org <mailto:Interest at qt-project.org>
>     https://lists.qt-project.org/listinfo/interest
> 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
> 




More information about the Interest mailing list