Document that QtConcurrent::run doesn't support overloaded functions

After improving QtConcurrent::run() to use parameter packs for the
arguments (see c977e74afd18afff8729070f631e6b7a3f2887f5), calling
overloaded functions is ambiguous. Updated the porting guide and the
documentation to mention this and describe possible workarounds.

Task-number: QTBUG-89648
Change-Id: I4c1f996ae67bce8c13cc1f99f54240295db6ae1d
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
(cherry picked from commit f1f4fd16fdd219701261d305e6d9f7abcb8bf4a9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Sona Kurazyan 2020-12-22 13:06:25 +01:00 committed by Qt Cherry-pick Bot
parent b14a33ff67
commit b02ed9696f
3 changed files with 43 additions and 0 deletions

View File

@ -230,3 +230,21 @@ run<double>(f); // this will select the 2nd overload
// run(f); // error, both candidate overloads potentially match // run(f); // error, both candidate overloads potentially match
//! [14] //! [14]
//! [15]
void foo(int arg);
void foo(int arg1, int arg2);
...
QFuture<void> future = QtConcurrent::run(foo, 42);
//! [15]
//! [16]
QFuture<void> future = QtConcurrent::run([] { foo(42); });
//! [16]
//! [17]
QFuture<void> future = QtConcurrent::run(static_cast<void(*)(int)>(foo), 42);
//! [17]
//! [18]
QFuture<void> future = QtConcurrent::run(qOverload<int>(foo), 42);
//! [18]

View File

@ -71,6 +71,11 @@
QFuture<void> future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba); QFuture<void> future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba);
\endcode \endcode
Another side effect is that \c QtConcurrent::run() will not work with
overloaded functions anymore. For example, the code below won't compile:
\include qtconcurrentrun.cpp run-with-overload-calls
Other methods of QtConcurrent have no behavioral changes and do not introduce Other methods of QtConcurrent have no behavioral changes and do not introduce
source compatibility breaks. source compatibility breaks.
*/ */

View File

@ -89,6 +89,26 @@
the function. Changes made to the arguments after calling the function. Changes made to the arguments after calling
QtConcurrent::run() are \e not visible to the thread. QtConcurrent::run() are \e not visible to the thread.
Note that QtConcurrent::run does not support calling overloaded functions
directly. For example, the code below won't compile:
//! [run-with-overload-calls]
\snippet code/src_concurrent_qtconcurrentrun.cpp 15
The easiest workaround is to call the overloaded function through lambda:
\snippet code/src_concurrent_qtconcurrentrun.cpp 16
Or you can tell the compiler which overload to choose by using a
\c static_cast:
\snippet code/src_concurrent_qtconcurrentrun.cpp 17
Or qOverload:
\snippet code/src_concurrent_qtconcurrentrun.cpp 18
//! [run-with-overload-calls]
\section2 Returning Values from the Function \section2 Returning Values from the Function
Any return value from the function is available via QFuture: Any return value from the function is available via QFuture: