How to use QtConcurrent::run with overloaded Funct

2019-07-29 06:28发布

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条回答
老娘就宠你
2楼-- · 2019-07-29 06:43

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.

查看更多
登录 后发表回答