3
林欲以std::bind
結合this
到方法,該方法在QtConcurrent::blockingMapped
的std ::綁定,這和QtConcurrent
部首使用:
class TuringMachine
{
private:
TRTable table;
std::set<ConfigNode*> currentConfigs;
//function object
std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)> step_f;
//method it will hold
std::set<ConfigNode *> step(TuringMachine* this_m ,ConfigNode *parent);
std::set<ConfigNode*>& makeStep();
}
來源:
TuringMachine::TuringMachine(/**/)
{
step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1);
}
std::set<ConfigNode*> &TuringMachine::makeStep(){
auto configSet = QtConcurrent::blockingMapped<QLinkedList<std::set<ConfigNode*>>>(currentConfigs, step_f);//concurrent execution!
/**/
return currentConfigs;
}
std::set<ConfigNode*> TuringMachine::step(TuringMachine *this_m, ConfigNode * parent){ //the actual step
/**/
}
那麼,我在這裏做什麼是運行步驟conncurre blockingMapped
每個ConfigNode
在currentConfigs
。 Im使用std::bind
綁定this
到step
,因此它只需要一個參數,如blockingMapped
的文檔中所述。
即時得到
error: no match for call to '(std::_Bind<std::_Mem_fn<std::set<ConfigNode*> (TuringMachine::*)(TuringMachine*, ConfigNode*)>(TuringMachine*, std::_Placeholder<1>)>) (const TuringMachine*, ConfigNode*)'
.../Qt/474/gcc/include/QtCore/qtconcurrentmapkernel.h:121: error: no match for call to '(std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)>) (ConfigNode* const&)'
.../Qt/474/gcc/include/QtCore/qtconcurrentmapkernel.h:136: error: no match for call to '(std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)>) (ConfigNode* const&)'
而且note: 2 arguments expected, 1 provided
哪兒我去錯了嗎?
編輯
更正,工作版本(未來 「參考」):
頁眉:
//function object
std::function<std::set<ConfigNode*>(ConfigNode*)> step_f;
//method it will hold
std::set<ConfigNode *> step(ConfigNode *parent);
來源:
TuringMachine::TuringMachine(/**/)
{
step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1);
}
爲什麼需要第二個'this'? – TeaOverflow
@Evgeni首先這是調用成員函數所必需的,因爲這樣做時,第二個參數需要是指向您的成員函數所源自的類的指針或您的類的對象。你可以例如說'std :: bind(&foo :: bar,bar())'。 – inf
那麼,我基本上試圖在幾個'ConfigNode'對象上同時運行'step'。 'blockingMapped'似乎是一個好主意,因爲它就是這樣做的。但我不能使用成員函數,如[這裏]所示(http://doc.qt.digia.com/4.7-snapshot/qtconcurrentmap.html#using-bound-function-arguments)。我必須綁定它或使其成爲靜態/全局。 – TeaOverflow