mardi 15 mai 2018

Is this a sensible pattern for reusing a C++11 lambda within a calling function?

I have a lambda that I wish to use more than once, but I want it to operate on at least one different (capture) variable the second time around. Is this approach - using a captured pointer ref inside the lambda that is redirected in the calling code to a different variable - a normal/sensible way to do it? Or should I be using a separate mechanism, or a different approach altogether?

MyStruct ms1;
MyStruct* pActiveMyStruct = &ms1;
auto lambda = [&]( const Foo& foo, u32 index )
{
    pActiveMyStruct->sScore = foo.m_Score;
    pActiveMyStruct->DoSomethingWith( index );
};
pProcessor->ApplyLambdaOn( lambda );

MyStruct ms2;
pActiveMyStruct = &ms2;
pProcessor->ApplyLambdaOn( lambda );

Aucun commentaire:

Enregistrer un commentaire