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

查看:104
本文介绍了函数指针,瓶盖,和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.

感谢。

推荐答案

一个拉姆达(或关闭)封装了函数指针和变量。这就是为什么,在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;
};

我用匿名委托那里作为封闭件(它的语法更清晰一点,比拉姆达相当于接近C)中,其中俘获每种不超过(堆栈变量)插入闭合。当闭合进行评估时,每种不超过(其堆栈帧可能已被破坏)将继续被引用。如果我改变每种不超过的话,我改变比较:

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
}

虽然我可以定义一个函数指针需要两个参数:

though I could define a function pointer that takes 2 arguments:

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个参数。如果我希望这个函数指针传递给另一个函数,其中每种不超过不在范围内,我要么必须手动保持它活着通过它传递给每个函数链中,或者将其提升为一个全球性的。

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.

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

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