qobject_cast: add a couple of static_asserts

... for better error reporting.

Change-Id: I11afdef41f729b4357b743a3238c1f055a1d35d3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2023-09-19 14:27:01 +02:00
parent d3c6d40d5d
commit 9d029939fb

View File

@ -384,6 +384,8 @@ bool QObject::setProperty(const char *name, QVariant &&value)
template <class T>
inline T qobject_cast(QObject *object)
{
static_assert(std::is_pointer_v<T>,
"qobject_cast requires to cast towards a pointer type");
typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
static_assert(QtPrivate::HasQ_OBJECT_Macro<ObjType>::Value,
"qobject_cast requires the type to have a Q_OBJECT macro");
@ -393,6 +395,10 @@ inline T qobject_cast(QObject *object)
template <class T>
inline T qobject_cast(const QObject *object)
{
static_assert(std::is_pointer_v<T>,
"qobject_cast requires to cast towards a pointer type");
static_assert(std::is_const_v<std::remove_pointer_t<T>>,
"qobject_cast cannot cast away constness (use const_cast)");
typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
static_assert(QtPrivate::HasQ_OBJECT_Macro<ObjType>::Value,
"qobject_cast requires the type to have a Q_OBJECT macro");