Discussion:
[Development] constexpr and qMetaTypeId<>()
a***@fastmail.fm
2013-09-11 21:29:44 UTC
Permalink
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
Thiago Macieira
2013-09-12 02:12:26 UTC
Permalink
Post by a***@fastmail.fm
Hello,
I'd like to use qMetaTypeId<>() in a constexpr expression.
You can't. The types are registered at runtime.
Post by a***@fastmail.fm
Any help is greatly appreciated.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Stephen Kelly
2013-09-12 07:47:26 UTC
Permalink
Post by a***@fastmail.fm
Hello,
I'd like to use qMetaTypeId<>() in a constexpr expression. I have my own
class MyCustomClass : public QObject
{
Q_OBJECT
// ...
};
Q_DECLARE_METATYPE(MyCustomClass*)
Unless you need to stay Qt 4 compatible, you can remove this line. Pointers to
QObject derived types are automatically treated as metatypes if needed.
Post by a***@fastmail.fm
constexpr qint32 metaTypeId = qMetaTypeId<MyCustomClass*>();
This only works for built-in metatypes which have an id value defined in the
QMetaType::Type enum.

Thanks,
--
Join us in October at Qt Developer Days 2013 - https://devdays.kdab.com

Stephen Kelly <***@kdab.com> | Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions
a***@fastmail.fm
2013-09-12 16:38:16 UTC
Permalink
Message: 1
Date: Wed, 11 Sep 2013 19:12:26 -0700
Subject: Re: [Development] constexpr and qMetaTypeId<>()
Content-Type: text/plain; charset="us-ascii"
Post by a***@fastmail.fm
Hello,
I'd like to use qMetaTypeId<>() in a constexpr expression.
You can't. The types are registered at runtime.
Post by a***@fastmail.fm
Any help is greatly appreciated.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Message: 5
Date: Thu, 12 Sep 2013 09:47:26 +0200
Subject: Re: [Development] constexpr and qMetaTypeId<>()
Content-Type: text/plain; charset="us-ascii"
Post by a***@fastmail.fm
Hello,
I'd like to use qMetaTypeId<>() in a constexpr expression. I have my own
class MyCustomClass : public QObject
{
Q_OBJECT
// ...
};
Q_DECLARE_METATYPE(MyCustomClass*)
Unless you need to stay Qt 4 compatible, you can remove this line.
Pointers to
QObject derived types are automatically treated as metatypes if needed.
Post by a***@fastmail.fm
constexpr qint32 metaTypeId = qMetaTypeId<MyCustomClass*>();
This only works for built-in metatypes which have an id value defined in
the
QMetaType::Type enum.
Thanks,
--
Join us in October at Qt Developer Days 2013 - https://devdays.kdab.com
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions
Thank you Thiago and Stephen for the information. I am, however, still
confused by both of your responses.

Thiago - The qMetaTypeId documentation
(http://qt-project.org/doc/qt-5.1/qtcore/qmetatype.html#qMetaTypeId)
specifically states that the type registration using this function is
resolved at compile-time, not run-time.

Stephen - The qMetaTypeId documentation goes on to provide a code
snippet where a custom type (MyStruct) is registered, so it would seem
this should work for non-built-in types as well.

As such, the documentation leads one to believe that qMetaTypeId can be
used to register one's custom type at compile-time, as I tried to do in
the code snippet in my original query.

Is the documentation completely wrong or is a compile-time type
registration still possible in some way? I am very interested in this,
as the system I am working on would greatly benefit from this
functionality.

Thanks again,

Alfonso
Thiago Macieira
2013-09-12 20:35:53 UTC
Permalink
Post by a***@fastmail.fm
Thiago - The qMetaTypeId documentation
(http://qt-project.org/doc/qt-5.1/qtcore/qmetatype.html#qMetaTypeId)
specifically states that the type registration using this function is
resolved at compile-time, not run-time.
The documentation is wrong. What Stephen said is correct.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Loading...