C++ 为mappedReduce指定reduce函子
我使用QtConcurrent并行运行某个任务。然而,当我真正改用mappedReduced而不是map时,我遇到了一个问题 以下是我使用的代码:C++ 为mappedReduce指定reduce函子,c++,qt,concurrency,C++,Qt,Concurrency,我使用QtConcurrent并行运行某个任务。然而,当我真正改用mappedReduced而不是map时,我遇到了一个问题 以下是我使用的代码: struct ProcMapWrapper { ConcurrentProcessTask *instance; ProcMapWrapper(ConcurrentProcessTask *w): instance(w) {} QString & operator()(const QString& data) {
struct ProcMapWrapper {
ConcurrentProcessTask *instance;
ProcMapWrapper(ConcurrentProcessTask *w): instance(w) {}
QString & operator()(const QString& data) {
return instance->map(data);
}
ProcMapWrapper(const ProcMapWrapper & src)
{
instance = src.instance;
}
typedef QString result_type;
};
struct ProcReduceWrapper {
ConcurrentProcessTask *instance;
ProcReduceWrapper(ConcurrentProcessTask *w): instance(w) {}
void operator()(int &number, const QString &fname) {
return instance->reduce(number,fname);
}
ProcReduceWrapper(const ProcReduceWrapper & src)
{
instance = src.instance;
}
typedef int result_type;
};
ProcMapWrapper mw(this);
ProcReduceWrapper rw(this);
futureWatcher.setFuture(QtConcurrent::mappedReduced(_files.begin(),_files.end(),mw,rw));
其思想是使用processOneItem函数从输入字符串生成stage1Data结构,然后在reduce函数中清除并将结果保存到适当的容器中
我做了一点研究,这让我想到了。其中,映射函子的模板如下所示:
struct Scaled
{
Scaled(int size)
: m_size(size) { }
typedef QImage result_type;
QImage operator()(const QImage &image)
{
return image.scaled(m_size, m_size);
}
int m_size;
};
QList<QImage> images = ...;
QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));
struct Scaled
{
缩放(整数大小)
:m_size(size){}
typedef QImage result_type;
QImage运算符()(常量QImage和图像)
{
返回图像。缩放(m_大小,m_大小);
}
国际货币单位大小;
};
QList图像=。。。;
QFuture thumbnails=QtConcurrent::mapped(图像,缩放(100));
这确实适用于QtConcurrent的map
和mapped
函数,但是我尝试使用相同的模式生成reduce函子,再次导致以下错误:
..\qt_photoaud\task.cpp(230):错误C2893:未能专门化
功能模板
'QFuture::ResultType>
QtConcurrent::mappedReduced(迭代器、迭代器、MapFunctor、ReduceFunctor、QtConcurrent::ReduceOptions)'
使用以下模板参数:
'QList::iterator'
具有
[
T=QString
]
“ProcMapWrapper”
“ProceduceWrapper”..\qt\u photoaud\task.cpp(230):错误C2783:“QFuture”
QtConcurrent::mappedReduced(迭代器、迭代器、MapFunctor、ReduceFunctor、QtConcurrent::ReduceOptions)'
:无法推断“ResultType”的模板参数
c:\qt\4.8.4\include\qtcore.././src/corelib/concurrent/qtccurrentmap.h(152):参见“QtConcurrent::mappedReduced”的声明
..\qt_photoaud\task.cpp(230):错误C2893:未能专门化
功能模板
'QFuture::ResultType>
QtConcurrent::mappedReduced(常量序列
&,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)'
使用以下模板参数:
'QList::iterator'
具有
[
T=QString
]
'QList::iterator'
具有
[
T=QString
]
'procmapprapper'..\qt_photoaud\task.cpp(230):错误C2783:'QFuture QtConcurrent::mappedReduced(常量序列
&,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)“”无法
推断“ResultType”的模板参数
c:\qt\4.8.4\include\qtcore.././src/corelib/concurrent/qtccurrentmap.h(125):参见'QtConcurrent::mappedReduced'的声明
reduce函子真的被定义和/或识别了吗?QtPrivate::ReduceResultType::ResultType是如何工作的?它需要什么
编辑:假设functor与boost::function兼容,我尝试在reduce functor中指定第一个参数类型、第二个参数类型、arg1类型和arg2类型。但这对我没有任何帮助。以下是对我有效的方法:
struct concurrentProcessDBMapper
{
typedef stage1Data result_type;
stage1Data operator()(QString fname)
{
return concurrentProcessDBMap(fname);
}
};
struct concurrentProcessDBReducer
{
DatabaseConcurrentProcessTask *instance;
concurrentProcessDBReducer(DatabaseConcurrentProcessTask *i)
{
instance = i;
}
void operator()(int & number, const stage1Data in)
{
//concurrentProcessDBReduce
instance->reduce(number,in);
}
};
...
QFutureWatcher<int> futureWatcher;
QList<QString> _filesInWork;
...
futureWatcher.setFuture(
QtConcurrent::mappedReduced
<int, QList<QString>, concurrentProcessDBMapper,concurrentProcessDBReducer>
(_filesInWork,concurrentProcessDBMapper(),concurrentProcessDBReducer(this)));
...
struct concurrentProcessDBMapper
{
类型定义阶段1数据结果类型;
stage1Data运算符()(QString fname)
{
返回concurrentProcessDBMap(fname);
}
};
结构concurrentProcessDBReducer
{
DatabaseConcurrentProcessTask*实例;
concurrentProcessDBReducer(数据库ConcurrentProcessTask*i)
{
实例=i;
}
void运算符()(int&number,const stage1数据输入)
{
//concurrentProcessDBReduce
实例->减少(数量,单位);
}
};
...
QFutureWatcher未来观察者;
QList_文件系统;
...
futureWatcher.setFuture(
QtConcurrent::mappedReduced
(_filesInWork,concurrentProcessDBApper(),concurrentProcessDBReducer(this));
...
总之,类型转换类似于-(map)->-(reduce)->,map functor将result\u类型定义为stage1Data,而reduce functor根本没有定义任何result\u类型。Future watcher将其类型定义为reduce result(即其first out参数的类型)——在我的例子中,mappedReduced的int和模板参数是明确定义的