C ++ - lambda expression, capture clause and class members

I use PPL and parallel_for for the syntax for the for loop. In the capture clause, I have 3 variables, one of which is a member of the class. There is a compilation error due to the presence of a class member among the variables in the capture clause. However, if I have this class member in a lambda body, it does not compile either, and the indicated error is that the variable in the enclosing area should be in the capture clause. How to act? Should I copy the variable element to a local variable in advance and pass it to the capture clause?

Here is the code with the formula Command member of the class.

parallel_for (m_rowStart,m_rowEnd+1,[&functionEvaluation,varModel_,formulaCommand](int i) { MLEquationVariableModel model_(varModel_); model_.addVariable("i", i); model_.addVariable("j", 1); MLEquationCommand* command_ = formulaCommand->duplicate(&model_); double d = command_->execute().toDouble(); if(d==NO_VALUE) { functionEvaluation.local() = NO_VALUE; } else { functionEvaluation.local() += d; } delete command_; }); 

Thanks!

+8
c ++ lambda parallel-processing ppl
source share
1 answer

You need to grab this to access member variables (remember that formulaCommand equivalent to this->formulaCommand ).

 [&functionEvaluation, varModel_, this](int i) { ... } 

(BTW, you should probably use a smart pointer ( unique_ptr<MLEquationCommand> ) instead of manually deleting the original command_ pointer.)

+7
source share

All Articles