On Wednesday 21 September 2016 11:42:41 Mathias Hasselmann wrote:
> Am 21.09.2016 um 09:52 schrieb Marc Mutz:
> > On Wednesday 21 September 2016 09:23:35 Mathias Hasselmann wrote:
> >> Maybe some clever use of type traits can tell us if your structs contain
> >> alignment wholes?
> > 
> > -Werror=padded :)
> 
> Yes, but how is it supposed to help? Let me show a real world example:
> 
>       struct Sensor
>       {
>           enum Config { NotInitialized, PullUp, PullDown, Analog };
> 
>           Config config;
>           QVariant value; //[1]
>       }
> 
> No matter what order I use for config and value, the compiler will pad
> and -Wpadded will complain. How am I supposed to fix this? This
> solutions that come to my mind all are ugly, but most likely I am just
> stupid.

Tabs? Seriously? :)

        struct Sensor
        {
            enum Config { NotInitialized, PullUp, PullDown, Analog };
 
            Config config;
            char padding[alignof(QVariant) - sizeof(Config)];
            QVariant value; //[1]
        }

and pray for alignof(QVariant) - sizeof(Config) != 0.

I wasn't serious about -Wpadded, but incidentally, my example shows that type 
traits also won't help. We have a handful of classes in Qt which have a 
'reserved' field that doesn't get initialized (so it better should be named 
'unusable'). A type trait cannot tell if a field called 'padding', 'reserved', 
'unused' or 'unusable' is meant to be part of equality or not.

Thanks,
Marc

-- 
Marc Mutz <marc.m...@kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - Qt, C++ and OpenGL Experts
_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to