使用 GCC 语句表达式的匿名函数 [英] Anonymous functions using GCC statement expressions

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

问题描述

这个问题不是很具体;这真的是为了我自己的 C 语言丰富,我希望其他人也能发现它很有用.

免责声明:我知道很多人会有反应如果你正在尝试做 FP,那么就使用函数式语言"的冲动.我在需要链接到许多其他 C 库的嵌入式环境中工作,并且没有太多空间容纳更多大型共享库,也不支持许多语言运行时.此外,动态内存分配是不可能的.我也很好奇.

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.

我们很多人都见过这个用于 lambda 表达式的漂亮的 C 宏:

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

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

一个示例用法是:

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

使用 gcc -std=c89 -E test.c,lambda 扩展为:

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. int (*X); 行究竟声明了什么?当然,int * X; 是一个整数指针,但是这两个有什么不同呢?

  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?

看看扩展后的宏,最终的 __fn__ 到底做了什么?如果我写一个测试函数 void test() { printf("hello");} test; - 立即抛出错误.我不明白那个语法.

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.

这对调试意味着什么?(我打算用这个和 gdb 来试验自己,但其他人的经验或意见会很棒).这会搞砸静态分析器吗?

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__;
    });

不是 C,而是有效的 GNU C.

is not C but is valid GNU C.

它使用了两个 gcc 扩展:

It makes use of two gcc extensions:

  1. 嵌套函数
  2. 语句表达式

嵌套函数(在复合语句中定义一个函数)和语句表达式(({}),基本上是一个产生a 值)在 C 中是不允许的,并且来自 GNU C.

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.

在语句表达式中,最后一个表达式语句是构造的值.这就是嵌套函数 __fn__ 出现在语句表达式末尾的原因.表达式中的函数指示符(最后一个表达式语句中的__fn__)通过通常的转换被转换为指向函数的指针.这是用于初始化函数指针 max 的值.

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 语句表达式的匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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