Note that there are some explanatory texts on larger screens.

plurals
  1. POFunctional programming in C with macro "Higher Order Function" generators
    text
    copied!<p>Pay attention carefully because this is a hell of a question ;-)</p> <p>I want to use template functions for generic collection actions (like search, foreach, etc.) in C while maintaining compiler static type checking. It is fairly straightforward while you're using simple callbacks like in this example:</p> <pre><code>#define MAKE_FOREACH(TYPE)\ void foreach_##TYPE (TYPE[n] array, int n, void(*f)(TYPE)) {\ for(int i = 0; i &lt; n; i++) {\ f(array[i]);\ }\ } </code></pre> <p>so you can do things like:</p> <pre><code>MAKE_FOREACH(int) MAKE_FOREACH(float) void intcallback(int x){ printf("got %d\n", x); } void floatcallback(float x){ printf("got %f\n", x); } int main(){ int[5] iarray = {1,2,3,4,5}; float[5] farray = {1.0,2.0,3.0,4.0,5.0}; foreach_int(iarray, 5, intcallback); foreach_float(farray, 5, floatcallback); } </code></pre> <p>If I'd like to implement callbacks with return types, for example to make a "map" function, I could do:</p> <pre><code>#define MAKE_MAP(TYPE, RTYPE)\ RTYPE* map_##TYPE (TYPE[n] array, int n, RTYPE(*f)(TYPE)) {\ RTYPE* result = (RTYPE*)malloc(sizeof(RTYPE)*n);\ for(int i = 0; i &lt; n; i++) {\ result[i]=f(array[i]);\ }\ } </code></pre> <p>So far, so good. The problem comes now, when I want my callback functions to accept any number of typed arguments.</p> <p>The idea is something like:</p> <pre><code>#define MAKE_MAP(TYPE, RTYPE, ...)\ RTYPE* map_##TYPE (TYPE[n] array, int n, RTYPE(*f)(TYPE, __VA_ARGS__), __VA_ARGS__) /*this would work for the declaration (because just the types would be enough) but the parameter names are missing :-s*/ \ {\ RTYPE* result = (RTYPE*)malloc(sizeof(RTYPE)*n);\ for(int i = 0; i &lt; n; i++) {\ result[i]=f(array[i], /*here the names of the parameters, in order*/);\ }\ } </code></pre> <p>so, as you can see, I could <em>declare</em> a map function as:</p> <pre><code>MAKE_MAP(int, float, char) </code></pre> <p>giving:</p> <pre><code>float* map_int(int[n] array, int n, float(*f)(int, char), char); </code></pre> <p>but I cannot figure how to <em>implement</em> the parameter passing with the preprocessor. Here is where I ask for your help, ideas and suggestions.</p> <p>(By the way, don't tell me to use a variadic function as template and passing a va_list argument to the callback, because all this stuff was because of the type checking :-p)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload