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

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

Task-number: QTBUG-89648
Pick-to: 6.0
Change-Id: I4c1f996ae67bce8c13cc1f99f54240295db6ae1d
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Sona Kurazyan 2020-12-22 13:06:25 +01:00
parent f3747c21d3
commit f1f4fd16fd
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
//! [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);
\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
source compatibility breaks.
*/

View File

@ -89,6 +89,26 @@
the function. Changes made to the arguments after calling
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
Any return value from the function is available via QFuture: