Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 lambdas to Function Pointer

Tags:

c++

c++11

I'm starting to develop applications using C++11 lambdas, and need to convert some types to function pointers. This works perfectly in GCC 4.6.0:

void (* test)() = []()
{
    puts("Test!");
};

test();

My problem is when I need to use function or method local variables within the lambda:

const char * text = "test!";

void (* test)() = [&]()
{
    puts(text);
};

test();

G++ 4.6.0 gives the cast error code:

main.cpp: In function 'void init(int)':
main.cpp:10:2: error: cannot convert 'main(int argc, char ** argv)::<lambda()>' to 'void (*)()' in initialization

If use auto, it works ok:

const char * text = "Test!";

auto test = [&]()
{
    puts(text);
};

test();

My question is: how can I create a type for a lambda with [&]? In my case, I can not use the STL std::function (because my program does not use C++ RTTI and EXCEPTIONS runtime), and It has a simple implementation of function to solve this problem?

like image 708
Raphael Basso Avatar asked Jun 12 '12 13:06

Raphael Basso


People also ask

How do you convert lambda to a function?

To create a lambda function first write keyword lambda followed by one of more arguments separated by comma ( , ), followed by colon a ( : ), followed by a single line expression. Here we are using two arguments x and y , expression after colon is the body of the lambda function.

What is the correct syntax for lambda expression in C++11?

Lambdas can both capture variables and accept input parameters. A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function. auto y = [] (int first, int second) { return first + second; };

What is a function pointer C++11?

As we know that pointers are used to point some variables; similarly, the function pointer is a pointer used to point functions. It is basically used to store the address of a function. We can call the function by using the function pointer, or we can also pass the pointer to another function as a parameter.

Can you pass lambda to function C++?

One of the new features introduced in Modern C++ starting from C++11 is Lambda Expression. It is a convenient way to define an anonymous function object or functor. It is convenient because we can define it locally where we want to call it or pass it to a function as an argument.


2 Answers

I can not use the STL std::function (because my program does not use C++ RTTI and EXCEPTIONS runtime)

Then you may need to write your own equivalent to std::function.

The usual implementation of type erasure for std::function doesn't need RTTI for most of its functionality; it works through regular virtual function calls. So writing your own version is doable.

Indeed, the only things in std::function that need RTTI are the target_type and target functions, which are not the most useful functions in the world. You might be able to just use std::function without calling these functions, assuming that the implementation you're using doesn't need RTTI for its usual business.

Typically, when you disable exception handling, the program simply shuts down and errors out when encountering a throw statement. And since most of the exceptions that a std::function would emit aren't the kind of thing you would be able to recover from (calling an empty function, running out of memory, etc), you can probably just use std::function as is.

like image 169
Nicol Bolas Avatar answered Sep 18 '22 00:09

Nicol Bolas


Only lambdas with no capture can be converted to a function pointer. This is an extension of lambdas for only this particular case [*]. In general, lambdas are function objects, and you cannot convert a function object to a function.

The alternative for lambdas that have state (capture) is to use std::function rather than a plain function pointer.


[*]: If the lambda that holds state could be converted to function pointer, where would the state be maintained? (Note that there might be multiple instances of this particular lambda, each one with it's own state that needs to be maintained separately)

like image 20
David Rodríguez - dribeas Avatar answered Sep 22 '22 00:09

David Rodríguez - dribeas