views:

187

answers:

2

In C++0x, I'm wondering what the type is of a lambda function. Specifically:

#include<iostream>

type1 foo(int x){
 return [x](int y)->int{return x * y;};
}

int main(){

 std::cout<<foo(3)(4);//would output 12

 type2 bar = foo(5);
 std::cout<<bar(6);//would output 30
 return 0;
}

What do I need to replace type1/type2 with to get the above to work? Hopefully you can see what I'm trying to accomplish, so even if this isn't possible by a direct replacement of type1 and type2, perhaps you can guide me in the right direction.

In other words:

  • How can I get a function to return an anonymous function?
  • How can I assign an anonymous function to a variable?

Thanks!

Edit: I'm compiling with visual studio 2010

+5  A: 

From Wikipedia:

Lambda functions are function objects of an implementation-dependent type; this type's name is only available to the compiler. If the user wishes to take a lambda function as a parameter, the type must be a template type, or it must create a std::function to capture the lambda value.

VC10 compiles this

//Beware, brain-compiled code ahead!
#include<iostream>
#include<functional>

std::function<int(int)> foo(int x)
{
    return [x](int y)->int{return x * y;};
}

int main(){

    std::cout<<foo(3)(4) << '\n';

    auto bar = foo(5);
    std::cout<<bar(6) << '\n';

    return 0;
}

and prints

12
30
sbi
This works perfectly, thanks! Any idea what `std::function` does to make this possible?
Cam
std::function does template magic, similar to what std::bind1st does, except std::function is much easier to use :-). It is based on the boost::function framework, which you can see here http://www.boost.org/doc/libs/1_43_0/doc/html/function/tutorial.html#id866455. Ultimately, std::function is creating a new class (using templates). This new class is using implicit casting to turn the lambda function into the new class.
Dragontamer5788
+11  A: 

You can never know the type of lambda function because what logically happens is the compiler generates a (local) class with the function call operator overloaded and a lexical closure is represented by data members of that (local) class. This is what logically happens for a lambda function such as:

auto foo = [](int x, int y) { return x + y; };

The compiler logically does this:

struct CompilerGeneratedName { void operator()(int x, int y) const { return x + y; } };
CompilerGeneratedName foo;

Since the compiler generates a (local) class, it generates a name and therefore you can never explicitly write the type, you can only deduce the type from either type deductions of template function arguments or using auto/decltype.

Also C++0x closures are statically allocated so you couldn't safely return a raw C++0x closure anyway.

Still there are few ways you can achieve this, the first is more flexible and supports lambda functions which capture lexical scopes. Use std::function, if you have a lambda function which isn't capturing anything from outer scope then you can use function pointers but this conversion is more for working with legacy code than anything.

So basically what you want is this:

std::function< int (int) > foo(int x)
{
    return [x](int y)->int{return x * y;};
}

The reason why I kept on saying logically, is because this is how boost::lambda kind of works originally (even though C++03 does not allow local classes to used in template function arguments) and where the idea of adding lambda functions originate from but since this is a language feature now compiler vendors could implement it in different and more efficient ways like when capturing all of the environment by reference the compiler can just pass a pointer to the call stack instead of the logical way while still maintaining the logical view.

snk_kid
Thanks, +1. What do you mean by "if you lambda function isn't capturing anything from outer scope then you can use function pointers."? Isn't it capturing the x?
Cam
@incrediman yes your example is capturing so you couldn't use function pointers, only lambda functions that are stateless can be converted to function pointers.
snk_kid
@snk_kid: Oh. I actually just misread what you wrote, it makes perfect sense the way it is :)
Cam
Still editing, I think conversion is the correct term since if I remember correctly a stateless lambda function has an implicit conversion to function pointer.
snk_kid
Yes it does (FCD *§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."*
Georg Fritzsche
Why do you have `foo` return `std::function<void ()>`? Shouldn't it be `std::function<int (int)>` ?
Motti
@Motti yes, I wasn't paying attention.
snk_kid