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

Marco Bubke Marco.Bubke at qt.io
Mon Dec 31 14:56:29 CET 2018


Is it not nice to share your knowledge why it is not possible? 😉 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?

________________________________
From: Development <development-bounces at qt-project.org> on behalf of Thiago Macieira <thiago.macieira at intel.com>
Sent: Monday, December 31, 2018 12:51:46 PM
To: development at qt-project.org
Subject: Re: [Development] Use QMetaEnum::keyCount() to initialise array

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



_______________________________________________
Development mailing list
Development at qt-project.org
https://lists.qt-project.org/listinfo/development
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20181231/ec96e710/attachment.html>


More information about the Development mailing list