问题
I'm currently trying to parallelize my code, therefore I'm using QtConcurrent::run
and the problem is, run doesn't know which function to choose.
Is there a way to use run with an overloaded function or do I have find some sort of workaround?
回答1:
You can just static_cast
the pointer to ensure there's no ambiguity in the process
void hello(QString name)
{
qDebug() << "Hello" << name << "from" << QThread::currentThread();
}
void hello(int age)
{
qDebug() << "Hello" << age << "from" << QThread::currentThread();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QFuture<void> f1 = run(static_cast<void(*)(QString)>(hello), QString("Alice"));
QFuture<void> f2 = run(static_cast<void(*)(int)>(hello), 42);
f1.waitForFinished();
f2.waitForFinished();
}
or alternatively get a pointer to the right one.
来源:https://stackoverflow.com/questions/40762460/how-to-use-qtconcurrentrun-with-overloaded-function