相关疑难解决方法(0)

有没有办法在C中做cur?

假设我有一个指向函数的指针_stack_push(stack* stk, void* el).我希望能够调用curry(_stack_push, my_stack)并获取刚刚执行的功能void* el.我想不出办法,因为C不允许运行时函数定义,但我知道有比我更聪明的人:).有任何想法吗?

c functional-programming currying

37
推荐指数
2
解决办法
7837
查看次数

C:未指定数量的参数 - void foo()

在这里读到的是C void foo()手段a function foo taking an unspecified number of arguments of unspecified type.

任何人都可以给我或指向一个C函数采用未指定数量的参数的示例吗?这适用于C什么?我在网上找不到任何东西.

谢谢!

c function variadic-functions

10
推荐指数
1
解决办法
5932
查看次数

使用联合和函数指针在 C 中实现函数委托

我希望能够将一个函数一般地传递​​给C 中的一个函数。我已经使用 C 几年了,我知道实现正确的闭包和高阶函数的障碍。这几乎是不可逾越的。

我搜索了 StackOverflow 以查看其他消息来源对此事的看法:

...除了使用可变参数或程序集之外,没有人有一个银弹一般的答案。我没有汇编的骨头,但如果我能在宿主语言中有效地实现一个特性,我通常会尝试。

因为我不能轻易拥有 HOF...

我喜欢高阶函数,但我会在紧要关头满足于委托。我怀疑通过类似下面的代码,我可以在 C 中获得一个可行的委托实现

想到了这样的实现:

enum FUN_TYPES {
    GENERIC,
    VOID_FUN,
    INT_FUN,
    UINT32_FUN,
    FLOAT_FUN,
};

typedef struct delegate {
    uint32 fun_type;
    union function {
        int (*int_fun)(int);
        uint32 (*uint_fun)(uint);
        float (*float_fun)(float);
        /* ... etc. until all basic types/structs in the 
           program are accounted for. */
    } function;
} delegate; …
Run Code Online (Sandbox Code Playgroud)

c delegates function-pointers anonymous-function unions

5
推荐指数
1
解决办法
1709
查看次数