mardi 22 août 2017

Capturing the 'this' pointer in an embedded lambda

I have a lambda called from another lambda, and in the 'inner' lambda I don't seem to be capturing the 'this' pointer correctly. Is there something wrong with my syntax here? My code is of the form:

void MyClass::myFunction(double newValue)
{
    auto innerLambda = [this]() -> bool    // I've also tried '[&]' here
                       { 
                           if (this -> State == State1)
                               return true;
                           else
                               return false;
                       };

     auto outerLambda = [&](double newValue)   
                        {
                            if (innerLambda())
                            {
                                // Do something
                                ...
                            }
                            else
                            {
                               // Do something else
                               ...
                            }
                        };  

     outerLambda(newValue);
}

If I put a debug breakpoint in 'outerLambda' and examine the contents of the 'this' pointer, then everything is present and correct, but if I do the same in 'innerLambda' then the 'this' pointer seems to be pointing to garbage.

Aucun commentaire:

Enregistrer un commentaire