Function returning a lambda expression

I wonder if it is possible to write a function that returns a lambda function in C ++ 11. Of course, one problem is how to declare such a function. Each lambda has a type, but this type is not expressed in C ++. I do not think this will work:

auto retFun() -> decltype ([](int x) -> int) { return [](int x) { return x; } } 

And this:

 int(int) retFun(); 

I do not know any automatic transformations from lambda, for example, function pointers or some such. Is this the only solution that processes the function object and returns it?

+83
c ++ function lambda c ++ 11
Jan 18 2018-11-18T00:
source share
5 answers

You don't need a function object created manually, just use std::function , into which you can convert lambda functions:

This example returns an integer identification function:

 std::function<int (int)> retFun() { return [](int x) { return x; }; } 
+93
Jan 18 '11 at 17:22
source share

For this simple example you do not need std::function .

From the standard ยง 5..1.2 / 6:

The closure type for a lambda expression without lambda capture has a public non-virtual implicit conversion function const, so that a pointer to a function that has the same parameter and return types as the closure function call statement. The value returned by this conversion function must be the address of a function that, when called, has the same effect as when calling the statement to call the close function.

Since your function has no capture, this means that lambda can be converted to a pointer to a function of type int (*)(int) :

 typedef int (*identity_t)(int); // works with gcc identity_t retFun() { return [](int x) { return x; }; } 

What I understand, correct me if I am wrong.

+29
Jan 19 2018-11-11T00:
source share

You can return the lambda function from another lambda function, since you do not have to explicitly specify the return type of the lambda function. Just write something like this in the global area:

  auto retFun = []() { return [](int x) {return x;}; }; 
+20
Nov 05 '11 at 17:27
source share

Although the question specifically asks the question about C ++ 11, for others who stumble over it and have access to the C ++ 14 compiler, C ++ 14 now allows you to infer return types for regular functions. Thus, the example in the question can be adjusted just to work as desired, simply by dropping the sentence -> decltype ... after the list of function parameters:

 auto retFun() { return [](int x) { return x; } } 

Note, however, that this will not work if more than one return <lambda>; appears in the function return <lambda>; . This is due to the fact that the restriction on the return of the return type is that all return statements must return expressions of the same type, but each lambda object gets its own unique type by the compiler, therefore the expressions return <lambda>; will have different types.

+15
Jun 17 '16 at 23:40
source share

You should write like this:

 auto returnFunction = [](int x){ return [&x](){ return x; }(); }; 

to get your return as a function, and use it like this:

 int val = returnFunction(someNumber); 
0
Aug 23 '19 at 16:26
source share



All Articles