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

Tom Isaacson Tom.Isaacson at navico.com
Sun Dec 30 20:28:47 CET 2018

>On 19/12/2018 11:09, 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()];
>> It seems like Q_ENUM declares functions with Q_DECL_CONSTEXPR in C++11 
>> but I can't figure out how to get this to work.
>Are you sure they're constexpr? From what I can see in Qt 5.12,
>keyCount() is not constexpr. It's just const.

You're right, keyCount() isn't. My confusion is that Q_ENUM declares its functions as constexpr:
	#define Q_ENUM(ENUM) \
	    friend Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { return &staticMetaObject; } \
	    friend Q_DECL_CONSTEXPR const char *qt_getEnumName(ENUM) Q_DECL_NOEXCEPT { return #ENUM; }
But I don't understand why if the functions that then use them aren't.

Tom Isaacson

More information about the Interest mailing list