I can easily do this:
auto f = []()->int { return 4; };
auto g = [f]()->int { return f(); });
int i = g();
Nevertheless, I cannot do this:
int (*f)() = []()->int { return 4; };
int (*g)() = [f]()->int { return f(); });
int i = g();
Why I got such message in MSVC?
error C2440: 'initializing' : cannot convert from 'ClassName::functionName::< lambda_b2eebcdf2b88a20d8b40b0a03c412089>' to 'int (__cdecl *)(void)'
This occurs on line:
int (*g)() = [f]()->int { return f(); });
How to do this properly?
In order to allow the ParentFunction to call the ChildFunction, we need to provide the ParentFunction with specific rights to call another lambda function. This can be done by adding specific policies to a role and then assign that role to the lambda function.
You have to deploy your API Gateway in order to invoke it from outside. In the API Gateway sections, you click on the "Actions" button and then on "Deploy API". After the deployment, you will receive an URL which you can call via CURL to invoke your Lambda.
Here is the python example of calling another lambda function and gets its response. There is two invocation type 'RequestResponse' and 'Event'. Use 'RequestResponse' if you want to get the response of lambda function and use 'Event' to invoke lambda function asynchronously.
int (*f)() = []()->int { return 4; };
is still fine because lambdas with empty capture lists implicitly convert to matching function pointers.
This (crucial) condition is however not met in the second line:
int (*g)() = [f]()->int { return f(); });
^
Thus the conversion fails.
If you want to store a lambda that captures something, you either need an std::function
, or deduce the type with auto
as you did before; whatever fits your usecase. Function pointers simply cannot do that (in C++11, for the future, see Yakk's answer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With