From 9d029939fb8aad4f61950da2d64cfa123d9b909a Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 19 Sep 2023 14:27:01 +0200 Subject: [PATCH] qobject_cast: add a couple of static_asserts ... for better error reporting. Change-Id: I11afdef41f729b4357b743a3238c1f055a1d35d3 Reviewed-by: Thiago Macieira Reviewed-by: Marc Mutz Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qobject.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index a7b2648e45..824a29a5a4 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -384,6 +384,8 @@ bool QObject::setProperty(const char *name, QVariant &&value) template inline T qobject_cast(QObject *object) { + static_assert(std::is_pointer_v, + "qobject_cast requires to cast towards a pointer type"); typedef typename std::remove_cv::type>::type ObjType; static_assert(QtPrivate::HasQ_OBJECT_Macro::Value, "qobject_cast requires the type to have a Q_OBJECT macro"); @@ -393,6 +395,10 @@ inline T qobject_cast(QObject *object) template inline T qobject_cast(const QObject *object) { + static_assert(std::is_pointer_v, + "qobject_cast requires to cast towards a pointer type"); + static_assert(std::is_const_v>, + "qobject_cast cannot cast away constness (use const_cast)"); typedef typename std::remove_cv::type>::type ObjType; static_assert(QtPrivate::HasQ_OBJECT_Macro::Value, "qobject_cast requires the type to have a Q_OBJECT macro");