From 357ca7d90a927b2f26139f30e59fcef36dca917d Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 27 Apr 2023 11:50:24 +0200 Subject: [PATCH] Tighten template on new QRunnable create method Only instantiate with object/methods that are invokable with void and returns void. Change-Id: Iab2e43bb8e061e3875a6cca8e06ebbfbfa9e6fe8 Reviewed-by: Marc Mutz --- src/corelib/thread/qrunnable.cpp | 5 ++++- src/corelib/thread/qrunnable.h | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/corelib/thread/qrunnable.cpp b/src/corelib/thread/qrunnable.cpp index 718802010a..068dc1af2a 100644 --- a/src/corelib/thread/qrunnable.cpp +++ b/src/corelib/thread/qrunnable.cpp @@ -78,13 +78,16 @@ QRunnable::~QRunnable() */ /*! - \fn template QRunnable *QRunnable::create(Callable &&callableToRun); + \fn template> QRunnable *QRunnable::create(Callable &&callableToRun); \since 5.15 Creates a QRunnable that calls \a callableToRun in run(). Auto-deletion is enabled by default. + \note This function participates in overload resolution only if \c Callable + is a function or function object which can be called with zero arguments. + \note In Qt versions prior to 6.6, this method took copyable functions only. \sa run(), autoDelete() diff --git a/src/corelib/thread/qrunnable.h b/src/corelib/thread/qrunnable.h index 131efdedb7..d8473a7f5f 100644 --- a/src/corelib/thread/qrunnable.h +++ b/src/corelib/thread/qrunnable.h @@ -24,6 +24,9 @@ public: static QRunnable *create(std::function functionToRun); #endif template + using if_callable = std::enable_if_t, bool>; + + template = true> static QRunnable *create(Callable &&functionToRun); bool autoDelete() const { return m_autoDelete; } void setAutoDelete(bool autoDelete) { m_autoDelete = autoDelete; } @@ -76,7 +79,7 @@ public: } }; -template +template > QRunnable *QRunnable::create(Callable &&functionToRun) { return new QGenericRunnable(