samedi 3 octobre 2015

C++ lambda - capture member variable

I have a class that has function pointer to kernel function, that can change from outside.

class Foo 
{
   public:
     std::function<double()> kernel;
     int i;         
};

int main()
{

  Foo f;
  f.kernel = []() -> double { return i * i; }; //this is not working obviously

}

How can I achieve behaviour that is "presented", eg. read class variables inside lambda. I can bypass it by passing f inside and write f.i, but that is not very nice solution and not working if i is isnide another struct.

Aucun commentaire:

Enregistrer un commentaire