lambda 函数的类型是什么? [英] What is the type of a lambda function?
问题描述
在 C++0x 中,我想知道 lambda 函数的类型是什么.具体:
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;
}
我需要用什么来替换 type1/type2 才能使上述工作正常工作?希望您能看到我正在努力完成的工作,因此即使通过直接替换 type1 和 type2 无法做到这一点,也许您可以指导我朝着正确的方向前进.
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.
换句话说:
- 如何让函数返回匿名函数?
- 如何将匿名函数分配给变量?
谢谢!
我正在使用 Visual Studio 2010 进行编译
I'm compiling with visual studio 2010
推荐答案
你永远无法知道 lambda 函数的类型,因为逻辑上发生的是编译器生成一个(本地)类,函数调用运算符重载,词法闭包是由该(本地)类的数据成员表示.这就是 lambda 函数的逻辑情况,例如:
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;
由于编译器生成一个(本地)类,它生成一个名称,因此您永远不能显式编写类型,您只能从模板函数参数的类型推导或使用 auto/decltype 推导类型.
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.
此外,C++0x 闭包是静态分配的,因此无论如何您都无法安全地返回原始 C++0x 闭包.
Also C++0x closures are statically allocated so you couldn't safely return a raw C++0x closure anyway.
仍然有几种方法可以实现这一点,第一种更灵活并且支持捕获词法作用域的 lambda 函数.使用 std::function,如果您的 lambda 函数没有从外部范围捕获任何内容,那么您可以使用函数指针,但这种转换更适合处理遗留代码而不是任何东西.
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;};
}
我一直在逻辑上说的原因是因为这就是 boost::lambda 最初的工作方式(即使 C++03 不允许在模板函数参数中使用本地类)以及添加的想法lambda 函数起源于但由于这是一种语言功能,现在编译器供应商可以以不同且更有效的方式实现它,例如通过引用捕获所有环境时,编译器可以只传递指向调用堆栈的指针而不是逻辑方式,同时仍然维护逻辑视图.
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.
这篇关于lambda 函数的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!