使用GCC声明前pressions匿名函数 [英] Anonymous functions using GCC statement expressions

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

问题描述

这问题不是非常具体的; 这真的对我自己的C富集,我希望其他人能够找到它也是有用的。

免责声明:我知道很多人都会有冲动与如果你试图做FP然后只用一个函数式语言回应。我需要链接到许多其他的C库,并没有什么太大的空间,更多的大型共享库,并且不支持多语言运行时的嵌入式环境中工作。此外,动态内存分配出了问题。我也只是真的很好奇。

我们很多人都看到了拉姆达前pressions这个漂亮的C代码宏:

 的#define拉姆达(return_type,FUNCTION_BODY)\\
({\\
      return_type __fn__ FUNCTION_BODY \\
          __fn__; \\
})

和一个例子用法是:

  INT(*最大值)(INT,INT)=拉姆达(INT(INT X,int y)对{返回X> Y X:Y?;});
最大值(4,5); //例

使用的gcc -std = C89 -E test.c以,拉姆达扩展为:

  INT(*最大值)(INT,INT)=({INT __fn__(INT X,int y)对{返回X> Y X:Y?;} __fn__;}) ;

因此​​,这些是我的问题:


  1. 什么precisely并行的 INT(* X); 的声明?当然,的 INT * X; 的是一个指向一个整数,但如何这两个不同的


  2. 在exapnded微距拍摄一看,是什么在地球上最后的 __ __ FN 吗?如果我写一个测试函数无效测试(){printf的(你好); }测试; - 这立即引发错误的。我不明白的语法。


  3. 这是什么意思进行调试? (我打算这个和gdb试验自己,但别人的经验或意见将是巨大的)。这会搞砸了静态分析?



解决方案

这个声明(在块范围内):

  INT(*最大值)(INT,INT)=
    ({
    INT __fn__(INT X,int y)对{返回X> ÿ? X:Y; }
    __fn__;
    });

不是C,但有效的GNU C。

它利用两个的gcc 扩展:


  1. 嵌套函数

  2. 声明前pressions

两者嵌套函数的(定义复合语句中的函数)和语句前pressions 的( {} ,基本上是产生一个值块)是不是在C允许的,来自GNU C。

在一份声明中前pression,最后前pression语句是建构的价值。这就是为什么嵌套函数 __ __ FN 出现在语句前pression的最后一个前pression声明。在EX pression函数指示符( __ FN __ 在过去的前pression语句)由通常的转换转换为指针的函数。这是用于初始化函数指针的值最大

This question isn't terribly specific; it's really for my own C enrichment and I hope others can find it useful as well.

Disclaimer: I know many will have the impulse to respond with "if you're trying to do FP then just use a functional language". I work in an embedded environment that needs to link to many other C libraries, and doesn't have much space for many more large shared libs and does not support many language runtimes. Moreover, dynamic memory allocation is out of the question. I'm also just really curious.

Many of us have seen this nifty C macro for lambda expressions:

#define lambda(return_type, function_body) \
({ \
      return_type __fn__ function_body \
          __fn__; \
})

And an example usage is:

int (*max)(int, int) = lambda (int, (int x, int y) { return x > y ? x : y; });
max(4, 5); // Example

Using gcc -std=c89 -E test.c, the lambda expands to:

int (*max)(int, int) = ({ int __fn__ (int x, int y) { return x > y ? x : y; } __fn__; });

So, these are my questions:

  1. What precisely does the line int (*X); declare? Of course, int * X; is a pointer to an integer, but how do these two differ?

  2. Taking a look at the exapnded macro, what on earth does the final __fn__ do? If I write a test function void test() { printf("hello"); } test; - that immediately throws an error. I do not understand that syntax.

  3. What does this mean for debugging? (I'm planning to experiment myself with this and gdb, but others' experiences or opinions would be great). Would this screw up static analyzers?

解决方案

This declaration (at block scope):

int (*max)(int, int) =
    ({
    int __fn__ (int x, int y) { return x > y ? x : y; }
    __fn__;
    });

is not C but is valid GNU C.

It makes use of two gcc extensions:

  1. nested functions
  2. statement expressions

Both nested functions (defining a function inside a compound statement) and statement expressions (({}), basically a block that yields a value) are not permitted in C and come from GNU C.

In a statement expression, the last expression statement is the value of the construct. This is why the nested function __fn__ appears as an expression statement at the end of the statement expression. A function designator (__fn__ in the last expression statement) in a expression is converted to a pointer to a function by the usual conversions. This is the value used to initialize the function pointer max.

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

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