Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of a lambda function?

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

like image 932
Cam Avatar asked Jul 01 '10 19:07

Cam


People also ask

What data type is lambda function in C++?

A lambda is an object (hence why we're referring to it as a functor, rather than a function) so has a type and can be stored. However, the type of the lambda is only known by the compiler (since it is compiler-generated), so you must use auto for declaration instances of the lambda.

What are Lambda functions in python?

Python Lambda Functions are anonymous function means that the function is without a name. As we already know that the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python.

What is the return type of lambda function expression?

The return type of a method in which lambda expression used in a return statement must be a functional interface.


1 Answers

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.

like image 199
snk_kid Avatar answered Oct 20 '22 20:10

snk_kid