[Development] Use QMetaEnum::keyCount() to initialise array

Jedrzej Nowacki Jedrzej.Nowacki at qt.io
Wed Jan 2 10:12:51 CET 2019


On Monday, December 31, 2018 2:56:29 PM CET Marco Bubke wrote:
> Is it because the meta type information could be created later? What about 
adding
> a constexpr version like: QMetaEnum::compileTimeFromType? Or do we wait for
> static reflections?

Yes, sometimes, but the fact that the output changes is more problematic. This 
would break if an enum form a dynamically linked library is used, for example:

 const QMetaEnum metaEnum = QMetaEnum::fromType<QIODevice::OpenModeFlag>();
 int MyArray[metaEnum.keyCount()];

metaEnum.keyCount() value would be inlined in an app and then a Qt update, 
which adds a new enum value, would break the app. Technically you could have 
something like that:

 QVarLengthArray<int,  
QMetaEnum::compileTimeFromType<QIODevice::OpenModeFlag>.keyCount>
 MyArray(metaEnum.keyCount());

but that would require new API convention and some research, because it looks 
like a nice source of bugs. Dropping BC would also help.

Cheers,
  Jędrek







More information about the Development mailing list