C ++ lambda表达式-编译器如何解释它们? [英] C++ lambda expressions - How does the compiler interpret them?
问题描述
我刚刚开始学习C ++ 11的新功能。我正在阅读有关C ++ Primer(Stanley Lippman)中的lambda的文章,并正在尝试它们。
I just starter learning the new features in C++ 11. I was reading about lambdas in C++ Primer (Stanley Lippman) and was experimenting with them.
我尝试了以下代码段:
auto func() -> int (*) (){
//int c=0;
return []()-> int {return 0;};
}
int main(){
auto p = func();
}
此代码编译良好。因此,我猜没有任何捕获的lambda只是由编译器作为普通函数生成的,我们可以使用指向它们的普通函数指针。
This code compiled fine. So I guess lambdas without any captures are just generated as normal functions by the compiler and we can use a normal function pointer to them.
现在,我更改了代码以使用捕获:
Now I changed the code to use captures:
auto func() -> int (*) (){
int c=0;
return [=]()-> int {return c;};
}
int main(){
auto p = func();
}
但这无法编译。使用g ++时出现以下编译错误:
But this failed to compile. I got the following compilation error while using g++:
main.cpp: In function ‘int (* func())()’:
main.cpp:6:31: error: cannot convert ‘func()::__lambda0’ to ‘int (*)()’ in return
return [=]()-> int {return c;};
从错误中我可以理解,它不是生成的普通函数,可能是具有重载的呼叫操作符的类。还是其他东西?
From the error I can understand that it is not a normal function that is generated and it might probably be a class with an overloaded call-operator. Or is it something else?
我的问题:编译器如何内部处理lambda?我该如何传递使用捕获的lambda,即func()的返回值应该是什么?我目前无法想到一个用例,在该用例中,我需要使用像这样的lambda,但我只想了解更多有关它们的信息。请帮忙。
My questions : How does the compiler handle lambdas internally? How should I pass around lambdas that use captures i.e. what should be the return value from func()? I cant currently think of a use case where I would need to use lambdas like this but I just want to understand more about them. Please help.
谢谢。
推荐答案
所有lambda都是函数对象具有定义为 closure type 且具有 operator()
成员的实现的类型。每个lambda表达式也都有自己独特的闭包类型。
All lambdas are function objects with implementation defined type called closure type with a operator()
member. Every lambda expression has it's own unique closure type, too.
没有捕获的Lambda可以转换为。
Lambdas without a capture can be converted to a function pointer. Whether or not compiler generates a normal function behind the scenes is an internal detail and shouldn't matter to you.
不可能返回在函数内部定义的lambda,这是内部细节,对您而言无关紧要。 。几乎没有什么可以防止这种情况的发生-您不知道lambda表达式类型的名称,不能在 decltype
内使用lambda表达式,正如已经说过的,两个lambda表达式(即使在词法上相同)也具有不同的类型。
It's not possible to return a lambda that's defined inside a function. There are few things that prevent this - you don't know the name of a type of lambda expression, you can't use a lambda expression inside a decltype
and as already said, two lambda expressions (even if lexically identical) have different types.
您可以使用 std :: function
:
std::function<int()> func()
{
int i = 0;
return [=]()-> int {return i;};
}
这种方式也适用于捕获。
This way works with captures, too.
或类似这样的东西:
auto f = []{ return 0; };
auto func() -> decltype(f)
{
return f;
}
编辑:即将推出的C ++ 1y标准(更具体地说,返回类型推导),但可以执行以下操作:
The upcoming C++1y standard (more specifically, return type deduction), however, will allow you to do this:
auto func()
{
int i = 42;
return [=]{ return i; };
}
这篇关于C ++ lambda表达式-编译器如何解释它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!