Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

c++11 - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...