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

Thiago Macieira thiago.macieira at intel.com
Mon Dec 31 12:51:46 CET 2018


On Sunday, 30 December 2018 17:43:21 -02 Tom Isaacson wrote:
> Is it possible to use QMetaEnum::keyCount() to initialise an array?
> Something like:
> 
>     const QMetaEnum metaEnum = QMetaEnum::fromType<MyArray>();
>     int MyArray[metaEnum.keyCount()];

Yes. This should compile with GCC and Clang. It uses a C99 feature that is 
allowed in their C++ compilers known as Variable Length Array. But the code is 
non-standard and may not compile in other compilers.

If you want to make it portable, use QVector, QVarLengthArray or std::vector.

> I don't know how the Meta-Object Compiler creates this but surely it's
> possible to change it to be constexpr?

No, it's not.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center






More information about the Development mailing list