lambda函数的类型是什么? [英] What is the type of a lambda function?

查看:297
本文介绍了lambda函数的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆