Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda calling another external lambda

Tags:

c++

c++11

lambda

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?

like image 714
no one special Avatar asked Sep 27 '16 17:09

no one special


People also ask

Can a lambda call another lambda?

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.

How do you call lambda from the outside?

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.

How do you trigger Lambda function from Lambda function?

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.


1 Answers

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).

like image 198
Baum mit Augen Avatar answered Oct 15 '22 10:10

Baum mit Augen