[Development] It could be a little bug

Nicolás Alvarez nicolas.alvarez at gmail.com
Fri Nov 1 23:40:59 CET 2013


2013/11/1 Marc Mutz <marc.mutz at kdab.com>:
> On Tuesday, October 29, 2013 01:21:08 Jiergir Ogoerg wrote:
>> Hi,
>> There's an enum:
>>
>> //==> code
>> enum TableState
>>     {
>>         UnsupportedLocale,
>>         EmptyTable,
>>         UnknownSystemComposeDir,
>>         MissingComposeFile,
>>         NoErrors
>>     };
>> //<== code
>>
>> and this:
>>
>> //==> code
>> bool cleanState() const { return ((m_state & NoErrors) == NoErrors); }
>> //<== code
>>
>> Shouldn't the latter be ?:
>> //==> code
>> bool cleanState() const { return (m_state == NoErrors); }
>> //<== code
>>
>> Found at
>> QTSRC/qtbase/src/plugins/platforminputcontexts/compose/generator/qtablegene
>> rator.h
>
> According to the standard, they are equivalent, since reading a value from an
> enum object that does have one of the declared enum values results in undefined
> behaviour. That said, we're violating that in many places in Qt, e.g. in
> QFlags, so it might be that the code in question also abuses m_state that way.
> in that case, the existing code would be more robust, but faces the compiler
> optimising away the & NoErrors due to the reason mentioned above.

This isn't a QFlags-like bitfield though, since the enum values are
not different bits. EmptyTable | UnknownSystemComposeDir ==
MissingComposeFile, which makes no sense; clearly it's not intended to
be used that way.

-- 
Nicolás



More information about the Development mailing list