C中的匿名函数 [英] Anonymous functions in C.

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

问题描述

Gnu C包含一些有趣的扩展,其中包括返回值的
复合语句。例如:


({int y = foo(); int z;

if(y> 0)z = y;否则z = -y ;

z;

})

大括号括起来的一个块可以出现在括号内

以形成一个块"返回"一个值。这在某些宏或其他应用程序中很方便




实际上这个构造仅仅是(并且没有更少)

比匿名函数。


匿名函数在调用qsort时非常方便,例如
,只需编写表达式就可以了

编译器在qsort算法中的每个点上扩展匿名函数(作为内联函数)。


这个,和其他扩展名在文件中公布

包括在修订版本中的潜在限制

ISO / EIC 98/99

http://www.open-std.org/jtc1/sc22/wg...docs/n1229.pdf


这是官方标准网站。


该文档中提到了其他Gnu扩展名,例如typeof

,例如lcc-win32实现的扩展名。

jacob

Gnu C features some interesting extensions, among others
compound statements that return a value. For instance:

({ int y = foo(); int z;
if (y>0) z = y; else z=-y;
z;
})
A block enclosed by braces can appear within parentheses
to form a block that "returns" a value. This is handy
in some macros, or in other applications.

Actually this construct is nothing more (and nothing less)
than anonymous functions.

Anonymous functions could be really handy in call to qsort,
for instance, where just writing an expression could allow
the compiler to expand the anonymous function at each point of
call (as an inline function) within the qsort algorithm.

This, and other extensions are published in a document
"Potential Extnsions for Inclusion in a revision of
ISO/EIC 98/99" available at

http://www.open-std.org/jtc1/sc22/wg...docs/n1229.pdf

That is the official standard site.

Other Gnu extensions are mentioned in that document, like typeof
for instance, an extension that also lcc-win32 implements.
jacob

推荐答案

jacob navia< ja *** @ jacob.remcomp.frwrites:
jacob navia <ja***@jacob.remcomp.frwrites:

Gnu C包含一些有趣的扩展,其中包括返回值的
复合语句。例如:


({int y = foo(); int z;

if(y> 0)z = y;否则z = -y ;

z;

})

大括号括起来的一个块可以出现在括号内

以形成一个块"返回"一个值。这在某些宏或其他应用程序中很方便




实际上这个构造仅仅是(并且没有更少)

比匿名函数。


匿名函数在调用qsort时非常方便,例如
,只需编写表达式就可以了

编译器在qsort算法中扩展匿名函数的每一点

调用(作为内联函数)。
Gnu C features some interesting extensions, among others
compound statements that return a value. For instance:

({ int y = foo(); int z;
if (y>0) z = y; else z=-y;
z;
})
A block enclosed by braces can appear within parentheses
to form a block that "returns" a value. This is handy
in some macros, or in other applications.

Actually this construct is nothing more (and nothing less)
than anonymous functions.

Anonymous functions could be really handy in call to qsort,
for instance, where just writing an expression could allow
the compiler to expand the anonymous function at each point of
call (as an inline function) within the qsort algorithm.



不,GNU C的复合语句不是匿名函数,因为

他们不接受参数。你怎么用复合语句来代替compar参数来调用qsort()

?如何将复合语句获得要比较的值?


我想GNU C的复合语句可以扩展为

匿名函数,但这不是正在提出的内容。

No, GNU C''s compound statements are not anonymous functions, because
they don''t take arguments. How would you write a call to qsort()
using a compound statement in place of the compar argument? How would
the compound statement obtain the values to be compared?

I suppose GNU C''s compound statements could be extended to act like
anonymous functions, but that''s not what''s being proposed.


这个和其他扩展在文档中发布

包含在

ISO / EIC 98/99修订版中的潜在限制

http://www.open-std.org/jtc1/sc22/wg...docs/n1229.pdf


这是官方标准网站。


该文档中提到了其他Gnu扩展名,例如typeof

,例如lcc-win32实现的扩展名。
This, and other extensions are published in a document
"Potential Extnsions for Inclusion in a revision of
ISO/EIC 98/99" available at

http://www.open-std.org/jtc1/sc22/wg...docs/n1229.pdf

That is the official standard site.

Other Gnu extensions are mentioned in that document, like typeof
for instance, an extension that also lcc-win32 implements.



这对comp.std.c更合适。我对文件有一些评论

;如果你在那里发帖,我会回复。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http ://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

This would be more appropriate for comp.std.c. I have some comments
on the document; if you post there, I''ll reply.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


jacob navia说:


< snip>
jacob navia said:

<snip>

该文件中提到了其他Gnu扩展名,例如typeof

例如,lcc-win32也实现的扩展。
Other Gnu extensions are mentioned in that document, like typeof
for instance, an extension that also lcc-win32 implements.



如果你的文章不是那么经常用于你的产品的广告,那么人们可能会更认真地对待它们。 br />

请注意,C没有匿名函数。如果你想将

匿名函数添加到标准C,comp.std.c是适当的

新闻组,可以在其中提出建议。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

If your articles were not so often thinly-veiled advertisements for your
product, perhaps people might take them a little more seriously.

Note that C does not have anonymous functions. If you want to add
anonymous functions to standard C, comp.std.c is the appropriate
newsgroup in which to suggest it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


在文章< -Z ****** ************************@bt.com> ;,

Richard Heathfield< rj*@see.sig.invalidwrote :
In article <-Z******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:

>请注意,C没有匿名函数。如果要将匿名函数添加到标准C,comp.std.c是适当的新闻组,可以在其中进行建议。
>Note that C does not have anonymous functions. If you want to add
anonymous functions to standard C, comp.std.c is the appropriate
newsgroup in which to suggest it.



但是如果你想讨论匿名函数可能是什么样的,那么它们将如何与其余的C一起使用,等等,不考虑

是否适合标准化它们,那么comp.lang.c似乎是合理的选择。


- - 理查德


-

在一些字母表中需要考虑多达32个字符

" ; - 1963年的X3.4。

But if you want to discuss what anonymous functions might be like, how
they would fit in with the rest of C and so on, without considering
whether it is appropriate to standardise them, then comp.lang.c seems
a reasonable choice.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


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

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