函数指针,Closures和Lambda [英] Function pointers, Closures, and Lambda

查看:129
本文介绍了函数指针,Closures和Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习了函数指针,当我准备K& R章节的主题时,第一件给我打电话的是嘿,这有点像闭合。我知道这个假设在某种程度上是根本上是错误的,之后在线搜索并不真的找到这个比较的任何分析。

I am just now learning about function pointers and as I was readying the K&R chapter on the subject the first thing that hit me was, "Hey, this is kinda like a closure." I knew this assumption is fundamentally wrong somehow and after a search online wasn't really to find any analysis of this comparison.

那么为什么C风格函数指针从根本上不同于闭合或lambda?就我可以告诉它与函数指针仍然指向一个定义的(命名)函数,而不是能够匿名定义该函数的事实。

So why are C style function pointer fundamentally different than closures or lambdas? As far as I can tell it has to do with the fact that the function pointer still points to a defined(named) function as opposed to being able to anonymously define the function.

为什么把一个函数传递给一个函数呢?在第二种情况下,函数是未命名的,而第一种函数只是一个正常的日常函数。

Why is passing a function to a function seen as more powerful in the second case, where it is unnamed, than the first where it is just a normal everyday function that is being passed?

请告诉我如何以及为什么我错误的比较这两者。

Please tell me how and why I am wrong to compare the two so closely.

谢谢。

推荐答案

lambda(或)封装函数指针和变量。这就是为什么在C#中你可以这样做:

A lambda (or closure) encapsulates both the function pointer and variables. This is why, in C#, you can do:

int lessThan = 100;
Func<int, bool> lessThanTest = delegate(int i) {
   return i < lessThan;
};

我使用一个匿名代理作为闭包(它的语法更清晰, lambda等效),它捕获了lessThan(一个堆栈变量)到闭包。当评估闭包时,lessThan(其堆栈帧可能已被破坏)将继续被引用。如果我改变lessThan,那么我改变比较:

I used an anonymous delegate there as a closure (it's syntax is a little clearer and closer to C than the lambda equivalent), which captured lessThan (a stack variable) into the closure. When the closure is evaluated, lessThan (whose stack frame may have been destroyed) will continue to be referenced. If I change lessThan, then I change the comparison:

int lessThan = 100;
Func<int, bool> lessThanTest = delegate(int i) {
   return i < lessThan;
};

lessThanTest(99); // returns true
lessThan = 10;
lessThanTest(99); // returns false

在C中,这是非法的:

BOOL (*lessThanTest)(int);
int lessThan = 100;

lessThanTest = &LessThan;

BOOL LessThan(int i) {
   return i < lessThan; // compile error - lessThan is not in scope
}

指针有两个参数:

int lessThan = 100;
BOOL (*lessThanTest)(int, int);

lessThanTest = &LessThan;
lessThanTest(99, lessThan); // returns true
lessThan = 10;
lessThanTest(100, lessThan); // returns false

BOOL LessThan(int i, int lessThan) {
   return i < lessThan;
}

但是,现在当我评估它时,我必须传递2个参数。如果我想把这个函数指针传递给另一个函数,其中lessThan不在范围内,我将不得不通过传递给链中的每个函数,或者通过提升它到一个全局,手动保持它活着。

But, now I have to pass the 2 arguments when I evaluate it. If I wished to pass this function pointer to another function where lessThan was not in scope, I would either have to manually keep it alive by passing it to each function in the chain, or by promoting it to a global.

虽然支持闭包的大多数主流语言都使用匿名函数,但没有必要。你可以有没有匿名函数的闭包,没有闭包的匿名函数。

Though most mainstream languages that support closures use anonymous functions, there is no requirement for that. You can have closures without anonymous functions, and anonymous functions without closures.

摘要:闭包是函数指针+捕获的变量的组合。

Summary: a closure is a combination of function pointer + captured variables.

这篇关于函数指针,Closures和Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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