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

Thiago Macieira thiago.macieira at intel.com
Wed Jan 2 12:45:29 CET 2019

On Monday, 31 December 2018 11:56:29 -02 Marco Bubke wrote:
> 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?

Because the information is not known to the compiler at compile time. You need 
moc's parsing of the header in order for it to count how many enumerators are 
there in the enumeration. Like you said, if we had static reflections, we 
could do this entirely in the compiler -- we would do possibly ALL of moc as 
part of reflections.

But until that happens, moc needs to run and will produce a .cpp with the 

If you did have access to moc's output, you could access the enumeration count 
in constexpr time. After all, QMetaEnum::keyCount is simply:

    const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2;
    return mobj->d.data[handle + offset];

Note that moc produces those arrays as "static const", not "constexpr", so it 
may not be accessible *today* at constexpr evaluation time. Or it could. I 
don't remember if "static const" counts as constant expression...

The problem is that you have to #include the moc's output in the TU that wants 
to create the OP's array. And it must be #include'd or built by the build-
system exactly *once*.

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

More information about the Development mailing list