How to use QtConcurrent::run with overloaded Function

只谈情不闲聊 提交于 2019-12-11 08:35:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!