How can I call the lambda function from another lambda function

Consider the class below using the A () method, which contains two lambda functions, a1 () and a2 (). I would like to be able to call a2 from inside a1. However, when I do this (second line inside a1), I get an error

Error: the variable "cannot be implicitly committed because the default capture mode was not set"

I do not understand this error message. What should I collect here? I understand that using [this] in a lambda definition gives me access to the methods of the foo class, but I don’t understand how to do what I want.

Thanks in advance for being right on this.

class foo
{
   void A()
   {
      auto a2 = [this]() -> int
      {
         return 1;
      };

      auto  a1 = [this]() -> int
         {
            int result;
            result = a2();
            return result;
         };

      int i = a1();
      int j = a2();
   }
};
+4
source share
1 answer

a2 odr-use a2 a1. this a2; this . , a2 , =, & .

[this, &a2]  // capture a2 by reference
[this, &]    // capture all odr-used automatic local variables by reference, including a2
+6

Source: https://habr.com/ru/post/1689597/


All Articles