【发布时间】:2021-05-11 20:11:37
【问题描述】:
我想将 Qt 的 mappedReduced 与 lambda 一起使用。
对Make QtConcurrent::mapped work with lambdas 的回答似乎表明这是不可能直接实现的,但可以通过使用std::function 来实现。
虽然我可以通过这种方式构建编译器接受的对mapped() 的调用,但我无法弄清楚如何以编译器想要编译的方式将该原理应用于mappedReduced()。这应该如何表述?
简单示例:
int cap = 1;
std::function<int(char)> mlf = [cap](char c){ return static_cast<int>(c)+cap; };
std::function<void(double&,int const&)> rlf = [cap](double &d,int const &i){ d += static_cast<double>(i+cap); };
QVector<char> seq = {'a','b','c'};
QVector<int> v = QtConcurrent::blockingMapped( seq, mlf );
double d = QtConcurrent::blockingMappedReduced( seq, mlf, rlf );
[注意:最终代码必须支持捕获,因此我在此示例中包含了捕获。在这里删除捕获不会让编译器满意。]
IDE/编译器抱怨:
qtsupplement.cpp:291:17: error: no matching function for call to 'blockingMappedReduced'
qtconcurrentmap.h:199:12: note: candidate template ignored: couldn't infer template argument 'ResultType'
qtconcurrentmap.h:213:65: note: candidate template ignored: substitution failure [with MapFunctor = std::function<int (char)>, ReduceFunctor = std::function<void (double &, const int &)>, Sequence = QVector<char>]: implicit instantiation of undefined template 'QtPrivate::ReduceResultType<std::function<void (double &, const int &)> >'
qtconcurrentmap.h:228:12: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qtconcurrentmap.h:243:65: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
而'ResultType'应该是变量d的类型,rlf()的第一个参数也暗示了结果类型。
使用的版本:qt5.5.1、g++5.4.0、qtcreator4.9.0
【问题讨论】:
标签: qt c++11 lambda qt5 qtconcurrent