Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of lambda expression to function pointer

Tags:

This is a follow up question to this question: Lambda how can I pass as a parameter

MSDN supposedly has marked the item as fixed. I took a look at the specifications, but I'm having trouble converting their specifications into what the syntax should be.

So for example:

void printOut(int(*eval)(int)) {     for(int x = 0; x < 4; ++x)     {         std::cout << eval(x) << std::endl;     } } 

Now say I have the lambda:

auto lambda1 = [](int x)->int{return x;}; 

What is the syntax to convert lambda1 into the functional pointer equivalent so it can be passed to printOut?

Also, what about lambdas which actually have something in the brackets? For example:

int y = 5; auto lambda2 = [y](void)->int{return y;}; 

If this kind of lambda can't be converted to a function pointer, is there an alternative method for passing this type of lambda expression to printOut (or even a modified version of printOut, if so what's the syntax)?

like image 926
helloworld922 Avatar asked May 02 '11 03:05

helloworld922


People also ask

Is lambda a function pointer?

Lambda expressions, even captured ones, can be handled as a function pointer (pointer to member function). It is tricky because an lambda expression is not a simple function. It is actually an object with an operator().

Is lambda a Pythonic?

Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).

How do you declare a function pointer in C++?

We declare the function pointer, i.e., void (*ptr)(char*). The statement ptr=printname means that we are assigning the address of printname() function to ptr. Now, we can call the printname() function by using the statement ptr(s).

How do you pass lambda function in CPP?

Only variables that are mentioned in the lambda body are captured when a capture-default is used. To use lambda expressions in the body of a class member function, pass the this pointer to the capture clause to provide access to the member functions and data members of the enclosing class.


1 Answers

There is no syntax per se, it's an implicit conversion. Simply cast it (explicitly or implicitly) and you'll get your function pointer. However, this was fixed after Visual Studio 2010 was released, so is not present.


You cannot make a capture-full lambda into a function pointer ever, as you noted, so it's the function printOut that'll have to change. You can either generalize the function itself:

// anything callable template <typename Func> void printOut(Func eval)  {     // ... } 

Or generalize the function type in particular:

// any function-like thing that fits the int(int) requirement void printOut(std::function<int(int)> eval)  {     // ... } 

Each has their own trade-off.


†As far as I know, it's unknown of we'll get it in a service pack, or if we need to wait until a new release.

like image 162
GManNickG Avatar answered Sep 28 '22 02:09

GManNickG