Function returning a lambda expression
Asked Answered
S

6

108

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?

Sapphism answered 18/1, 2011 at 17:1 Comment(6)
To add what's already been said, stateless lambda functions are convertible to function pointers.Strangeness
IMO your first option won't work since the lambda in the decltype isn't the same as in the function body and therefore has a different type (even if you included the return statement)Subtotal
By the way, if a lambda has an empty capture clause, it can be implicitly convertible to a pointer to function.Jeremie
@GMan: Unless you are using Visual C++ 2010 or a version of g++ released more than about a year ago (or thereabouts). The captureless-lambda implicit conversion to function pointer wasn't added until March 2010 in N3092.Outstand
Lambda expressions in general cannot appear in unevaluated operands. So decltype([](){}) or sizeof([]() {}) is ill-formed no matter where you write it.Tellurian
One can use the + operator to convert a lambda to a function pointer.Lemay
P
112

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; };
}
Photoactive answered 18/1, 2011 at 17:22 Comment(5)
That is going to cause a memory allocation though in the constructor of std::function.Copenhagen
@Maxim Yegorushkin std::function has move semantics plus it can use custom allocators and the C++0x working draft has these notes: "[Note: implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, where f’s target is an object holding only a pointer or reference to an object and a member function pointer. —end note ]" so basically you can not make many assumptions as to what allocation strategy a particular implementation is using but you should be able to use your own (pooled) allocators anyway.Strangeness
@Sean: you could as well wrap it into boost::any. The question was how to specify the return type. This answer sidesteps the question.Copenhagen
@Maxim: My answer was to the question "Is the only solution handcrafting a function object and returning it?"Photoactive
Just keep in mind that std::function employs type erasure, which can mean the cost of making a virtual function call when calling the std::function. Something to be aware of if the returned function is going to be used in a tight inner loop or other context where the slight inefficiency matter.Steviestevy
M
31

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.

Magnanimity answered 19/1, 2011 at 0:44 Comment(6)
This sounds right. Unfortunately, it doesn't work with the current compiler I'm using: VS 2010. std::function conversion happens to work.Sapphism
Yes, the final wording of this rule came too late for VC2010.Microlith
I've added code example. Here's a full program.Stove
@J.F.Sebastian - What's the lifetime of the lambda in this example? Is it long enough to outlive the result of the conversion to function pointer?Cerement
@awoodland: Good question. The code does what the words in the answer say. Whether it is correct I don't know. The full program I've linked above works with gcc.Stove
@J.F.Sebastian - I can't seem to find an answer to that so I've asked it as a question in its own right: #8026670Cerement
S
30

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.

Steviestevy answered 17/6, 2016 at 23:40 Comment(1)
Why mention c++14's deduced types but omit polymorphic lambdas? auto retFun() { return [](auto const& x) { return x; }; }Isaacisaacs
M
24

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;};
 };
Marutani answered 5/11, 2011 at 17:27 Comment(3)
That's only true when the outer lambda consists of just the return statement. Otherwise you have to specify the return type.Sapphism
This is the best answer as it doesn't require runtime polymorphism of std::function and allows for lambda to have a non-empty capture list, however I would use const auto fun = ...Later
@BartoszMilewski not true since C++14.Marutani
C
1

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);
Commonwealth answered 23/8, 2019 at 16:26 Comment(0)
C
0

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();
}
Crawly answered 27/7, 2020 at 17:33 Comment(1)
How does this code compile if you do not have c++11? (lambdas are since c++11)Pyrexia

© 2022 - 2024 — McMap. All rights reserved.