1

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?

Marco A.
  • 43,032
  • 26
  • 132
  • 246
Dänis
  • 29
  • 3

1 Answers1

3

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.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • ok, does this work for an overload like: void hello(QString name, int age){..} and void hello(QString name){..} too? – Dänis Nov 23 '16 at 12:41
  • ok figured it out myself, just had to use the namespace in front of the function, instead of the static member, which called it – Dänis Nov 23 '16 at 13:47