Function returning a lambda expression

C++FunctionC++11Lambda

C++ Problem Overview


I wonder if it's possible to write a function that returns a lambda function in C++11. Of course one problem is how to declare such function. Each lambda has a type, but that type is not expressible in C++. I don't think this would work:

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

Nor this:

int(int) retFun();

I'm not aware of any automatic conversions from lambdas to, say, pointers to functions, or some such. Is the only solution handcrafting a function object and returning it?

C++ Solutions


Solution 1 - C++

You don't need a handcrafted function object, just use std::function, to which lambda functions are convertible:

This example returns the integer identity function:

std::function<int (int)> retFun() {
    return [](int x) { return x; };
}

Solution 2 - C++

For this simple example, you don't need std::function.

From standard §5.1.2/6:

> The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.

Because your function doesn't have a capture, it means that the lambda can be converted to a pointer to function of type int (*)(int):

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

That's my understanding, correct me if I'm wrong.

Solution 3 - C++

Though the question specifically asks about C++11, for the sake of others who stumble upon this and have access to a C++14 compiler, C++14 now allows deduced return types for ordinary functions. So the example in the question can be adjusted just to work as desired simply by dropping the -> decltype... clause after the function parameter list:

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

Note, however, that this will not work if more than one return <lambda>; appears in the function. This is because a restriction on return type deduction is that all return statements must return expressions of the same type, but every lambda object is given its own unique type by the compiler, so the return <lambda>; expressions will each have a different type.

Solution 4 - C++

You can return lambda function from other lambda function, since you should not explicitly specify return type of lambda function. Just write something like that in global scope:

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

Solution 5 - C++

You should write like this:

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

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

int val = returnFunction(someNumber);

Solution 6 - C++

If you do not have c++ 11 and are running your c++ code on micro controllers for example. You can return a void pointer and then perform a cast.

void* functionThatReturnsLambda()
{
	void(*someMethod)();

	// your lambda
	someMethod = []() {

		// code of lambda

	};

	return someMethod;
}


int main(int argc, char* argv[])
{

	void* myLambdaRaw = functionThatReturnsLambda();

	// cast it
	auto myLambda = (void(*)())myLambdaRaw;

	// execute lambda
	myLambda();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBartosz MilewskiView Question on Stackoverflow
Solution 1 - C++SeanView Answer on Stackoverflow
Solution 2 - C++user534498View Answer on Stackoverflow
Solution 3 - C++Anthony HallView Answer on Stackoverflow
Solution 4 - C++dzhioevView Answer on Stackoverflow
Solution 5 - C++Terens TareView Answer on Stackoverflow
Solution 6 - C++Tono NamView Answer on Stackoverflow