Yep documentation of QtConcurrent is bad. Here is better description how mappedReduced should be used.
So reduce function should look like:
void joinVectors(QVector<double> &reduceResult, const QVector<double> &partial) {
reduceResult += partial;
}
To keep proper order of data you should add extra argument:
res = QtConcurrent::mappedReduced(list_of_vectors, doSomeMapping, joinVectors, QtConcurrent::SequentialReduce);
Here is code I used for testing and it works (builds successfully and gives expected result):
#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include <QDebug>
QVector<double> addIndexToVector(QVector<double> vector) {
for (int i = 0; i < vector.size(); i++) {
vector[i] = vector[i] + i;
}
return vector;
}
void joinVectors(QVector<double> &reduceResult, const QVector<double> &partial) {
reduceResult += partial;
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QVector<double> vector_of_doubles(20, 0);
QList< QVector<double> > list_of_vectors;
list_of_vectors.append(vector_of_doubles);
list_of_vectors.append(vector_of_doubles);
list_of_vectors.append(vector_of_doubles);
QFuture< QVector<double> > res;
res = QtConcurrent::mappedReduced(list_of_vectors, addIndexToVector, joinVectors);
res.waitForFinished();
qDebug()<<res.result();
return a.exec();
}
Project file:
QT += core concurrent
QT -= gui
TARGET = testApp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
I used Qt 5.0.1 Linux version.