有没有办法做到用C钻营? [英] Is there a way to do currying in C?

查看:109
本文介绍了有没有办法做到用C钻营?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个指向函数 _stack_push(堆栈* STK,无效* EL)。我希望能够调用咖喱(_stack_push,my_stack)并取回一个函数,只需要无效* EL 。我不能想办法做到这一点,因为C语言不允许运行时函数的定义,但我知道这里有远远聪明人比我:)。任何想法?

Say I have a pointer to a function _stack_push(stack* stk, void* el). I want to be able to call curry(_stack_push, my_stack) and get back a function that just takes void* el. I couldn't think of a way to do it, since C doesn't allow runtime function definition, but I know there are far cleverer people than me here :). Any ideas?

推荐答案

我发现了一个纸洛朗大米厂,讨论在C / C ++ / Objective-C的柯里:

I found a paper by Laurent Dami that discusses currying in C/C++/Objective-C:

在C / C ++ / Objective-C的咖喱功能的更多功能的可重用性

感兴趣的是如何用C实现的:

Of interest to how it is implemented in C:

我们的当前实现使用现有的C结构,以增加柯里化机制。这是比修改编译器更容易做到,并且足以证明柯里的利益。这种方法有两个缺点,但是。首先,咖喱功能无法进行类型检查,因此需要以避免错误谨慎使用。其次,咖喱函数不能知道它的参数的大小,并计算它们,好像他们是所有的整数的大小

Our current implementation uses existing C constructs to add the currying mechanism. This was much easier to do than modifying the compiler, and is sufficient to prove the interest of currying. This approach has two drawbacks, however. First, curried functions cannot be type-checked, and therefore require careful use in order to avoid errors. Second, the curry function cannot know the size of its arguments, and counts them as if they were all of the size of an integer.

该文件不包含咖喱()的实现,但你能想象使用的​​函数指针可变参数的函数的。

The paper does not contain an implementation of curry(), but you can imagine how it is implemented using function pointers and variadic functions.

这篇关于有没有办法做到用C钻营?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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