diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-04-27 14:53:40 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-04-28 07:45:22 +0200 |
commit | bd69821074d62a6e8b5eca56d7b9307e1b3e8645 (patch) | |
tree | d4cb51f5b9f97d6e06d94aad01a30a6e33abaa66 /src/network | |
parent | 10a850f584f2a3276737ca7621f380ebfd29b4ed (diff) | |
download | qtbase-bd69821074d62a6e8b5eca56d7b9307e1b3e8645.tar.gz |
Support free functions and const functors as callbacks
Amend 207aae5560aa2865ec55ddb9ecbb50048060c0c0, as code checker
complained that we std::move'd a potential lvalue. This warning was
valid if the public API did not accept the functor parameter by value.
Fix this by consistently std::forward'ing the parameters through the
call stack, and add a compile-time test. Writing that test revealed that
the helper API didn't work with free functions, so fix that as well. It
also revealed that QFunctorSlotObject couldn't work with a const
functor, which is also fixed by this change.
We cannot support move-only functors with that change, as it requires
a change to QFunctorSlotObject that breaks the QMetaObject test.
Change-Id: Iafd747baf4cb0213ecedb391ed46b4595388182b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/kernel/qhostinfo.h | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/network/kernel/qhostinfo.h b/src/network/kernel/qhostinfo.h index d990af685a..9b420bd3ca 100644 --- a/src/network/kernel/qhostinfo.h +++ b/src/network/kernel/qhostinfo.h @@ -63,20 +63,20 @@ public: template <typename Functor> static inline int lookupHost(const QString &name, const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver, - Functor func) + Functor &&func) { using Prototype = void(*)(QHostInfo); return lookupHostImpl(name, receiver, - QtPrivate::makeSlotObject<Prototype>(std::move(func)), + QtPrivate::makeSlotObject<Prototype>(std::forward<Functor>(func)), nullptr); } #endif // Q_QDOC // lookupHost to a callable (without context) template <typename Functor> - static inline int lookupHost(const QString &name, Functor slot) + static inline int lookupHost(const QString &name, Functor &&slot) { - return lookupHost(name, nullptr, std::move(slot)); + return lookupHost(name, nullptr, std::forward<Functor>(slot)); } private: |