summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2023-04-27 11:50:24 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-04-27 22:55:28 +0000
commit357ca7d90a927b2f26139f30e59fcef36dca917d (patch)
tree33f411f0f6bf4dfd5cab200f106b3bdaeb07730d /src/corelib
parentebd2fe108ae4e72d94dd7e3bc0ed296253b68b3d (diff)
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 <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/thread/qrunnable.cpp5
-rw-r--r--src/corelib/thread/qrunnable.h5
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<typename Callable> QRunnable *QRunnable::create(Callable &&callableToRun);
+ \fn template<typename Callable, if_callable<Callable>> 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<void()> functionToRun);
#endif
template <typename Callable>
+ using if_callable = std::enable_if_t<std::is_invocable_r_v<void, Callable>, bool>;
+
+ template <typename Callable, if_callable<Callable> = 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 <typename Callable>
+template <typename Callable, QRunnable::if_callable<Callable>>
QRunnable *QRunnable::create(Callable &&functionToRun)
{
return new QGenericRunnable(