8

I am using PPL and parallel_for syntax to have a for loop. In the capture clause, I have 3 variables, one of them is a class member. There is a compilation error due to the presence of a class member among variables in the capture clause. However, if I have this class member in lambda body, it does not compile either, and error stated is that variable in enclosing scope should be in capture clause. How to proceed? Should I copy the variable member to a local variable beforehand, and have it passed in the capture clause?

Here is the code, with formulaCommand the class member.

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!

4

1 に答える 1

7

メンバー変数にアクセスするには、 capture が必要です (これは と同等thisであることを思い出してください)。formulaCommandthis->formulaCommand

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

(ところで、生のポインターunique_ptr<MLEquationCommand>を手動で処理する代わりに、おそらくスマート ポインター () を使用する必要があります。)deletecommand_

于 2012-04-30T07:30:18.107 に答える