Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a lambda function from another lambda function

Tags:

c++

lambda

Consider the contrived class below with a method A() that contains two lambda functions, a1() and a2(). I'd like to be able to invoke a2 from inside a1. However, when I do so, (the second line inside a1), I get the error

Error: variable “cannot be implicitly captured because no default capture mode has been specified”

I don't understand this error message. What am I supposed to be capturing here? I understand that using [this] in the lambda definition gives me access to methods in class foo but I'm unclear as to how to do what I want.

Thanks in advance for setting me straight 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();
   }
};
like image 643
David Avatar asked Nov 21 '17 00:11

David


People also ask

How do you call a lambda function from another account?

To have your Lambda function assume an IAM role in another AWS account, do the following: Configure your Lambda function's execution role to allow the function to assume an IAM role in another AWS account. Modify your cross-account IAM role's trust policy to allow your Lambda function to assume the role.

How do you call lambda from another lambda in Java?

The AWS Java SDK also provides a very handy class to invoke Lambda functions, InvokeRequest. We now need to create an object of this class, configure the Lambda function name, and also specify the payload. InvokeRequest request = new InvokeRequest(); request. withFunctionName(LAMBDA_FUNCTION_NAME).

How do you call another lambda function in Python?

If you want to orchestrate multiple lambda functions in your application, the best and recommended way is to use AWS Step Function. But if you just want to execute a lambda function from another lambda function, you can simply call the target lambda function from the source lambda function using AWS SDK.

Should lambdas call other lambdas?

It's generally frowned up for some good reasons, but as with most things, there are nuances to this discussion. In most cases, a Lambda function is an implementation detail and shouldn't be exposed as the system's API.


1 Answers

You need to capture a2 in order to odr-use a2 within the body of a1. Simply capturing this does not capture a2; capturing this only allows you to use the non-static members of the enclosing class. If you expect a2 to be captured by default, you need to specify either = or & as the capture-default.

[this, &a2]  // capture a2 by reference
[this, &]    // capture all odr-used automatic local variables by reference, including a2
like image 87
Brian Bi Avatar answered Oct 11 '22 08:10

Brian Bi