[Development] constexpr and qMetaTypeId<>()

achartier at fastmail.fm achartier at fastmail.fm
Wed Sep 11 23:29:44 CEST 2013


Hello,

I'd like to use qMetaTypeId<>() in a constexpr expression. I have my own
custom type declared as such:

class MyCustomClass : public QObject
{
     Q_OBJECT

     // ...
};

Q_DECLARE_METATYPE(MyCustomClass*)

I then try the following:

constexpr qint32 metaTypeId = qMetaTypeId<MyCustomClass*>();
static_assert(metaTypeId != 0, "boo, qMetaTypeId failed");

and I get the following compiler errors:

/opt/Qt/Qt5.1.1/5.1.1/gcc_64/include/QtCore/qmetatype.h:640: error:
'static constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T =
MyCustomClass*]' called in a constant expression
     return QMetaTypeId2<T>::qt_metatype_id();
                                            ^

/opt/Qt/Qt5.1.1/5.1.1/gcc_64/include/QtCore/qmetatype.h:532: 'static
constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T =
MyCustomClass*]' is not usable as a constexpr function because:
     static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return
     QMetaTypeId<T>::qt_metatype_id(); }
                                        ^

/opt/Qt/Qt5.1.1/5.1.1/gcc_64/include/QtCore/qmetatype.h:532: error: call
to non-constexpr function 'static int
QMetaTypeId<MyCustomClass*>::qt_metatype_id()'
     static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return
     QMetaTypeId<T>::qt_metatype_id(); }
                                                                                                 ^

Looking at the definitions for these functions in qmetatype.h, many of
these have the Q_DECL_CONSTEXPR macro, so I'm assuming this should work,
but it looks like QMetaTypeId<T>::qt_metatype_id() is not marked
constexpr even though all the other functions that rely on it in this
case are.

Question: is this a bug? Is a Q_DECL_CONSTEXPR missing somewhere? I was
unable to find the definition of QMetaTypeId<T>::qt_metatype_id() to
verify whether or not it is marked as constexpr. Given the prevalence of
Q_DECL_CONSTEXPR for many of these functions, I'm assuming the intent is
to be able to write something like the code I have written above, but
clearly, something isn't quite right.

Any help is greatly appreciated.

Thanks,

Alfonso



More information about the Development mailing list