Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing function delegates in C with unions and function pointers
    primarykey
    data
    text
    <p>I'd like to be able to <em>generically pass a function to a function</em> in C. I've used C for a few years, and I'm aware of the barriers to implementing proper closures and higher-order functions. It's almost insurmountable.</p> <p>I scoured StackOverflow to see what other sources had to say on the matter:</p> <ul> <li><a href="https://stackoverflow.com/questions/2535631/higher-order-functions-in-c">higher-order-functions-in-c</a></li> <li><a href="https://stackoverflow.com/questions/10405436/anonymous-functions-using-gcc-statement-expressions">anonymous-functions-using-gcc-statement-expressions</a></li> <li><a href="https://stackoverflow.com/questions/1023261/is-there-a-way-to-do-currying-in-c">is-there-a-way-to-do-currying-in-c</a></li> <li><a href="https://stackoverflow.com/questions/12991014/functional-programming-currying-in-c-issue-with-types">functional-programming-currying-in-c-issue-with-types</a></li> <li><a href="https://stackoverflow.com/questions/5524299/emulating-partial-function-application-in-c">emulating-partial-function-application-in-c</a></li> <li><a href="https://stackoverflow.com/questions/1650379/fake-anonymous-functions-in-c">fake-anonymous-functions-in-c</a></li> <li><a href="https://stackoverflow.com/questions/894167/functional-programming-in-c-with-macro-higher-order-function-generators">functional-programming-in-c-with-macro-higher-order-function-generators</a></li> <li><a href="https://stackoverflow.com/questions/14160516/higher-order-functions-in-c-as-a-syntactic-sugar-with-minimal-effort">higher-order-functions-in-c-as-a-syntactic-sugar-with-minimal-effort</a></li> </ul> <p>...and none had a silver-bullet generic answer, outside of either using varargs or assembly. I have no bones with assembly, but if I can efficiently implement a feature in the host language, I usually attempt to.</p> <h3>Since I can't have HOF easily...</h3> <p>I'd love higher-order functions, but I'll settle for delegates in a pinch. I suspect that with something like the code below I could get a workable <em>delegate implementation</em> in C.</p> <p>An implementation like this comes to mind:</p> <pre><code>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; </code></pre> <p>Usage Example:</p> <pre><code>void mapint(struct fun f, int arr[20]) { int i = 0; if(f.fun_type == INT_FUN) { for(; i &lt; 20; i++) { arr[i] = f.function.int_fun(arr[i]); } } } </code></pre> <p><br /></p> <p>Unfortunately, there are some obvious downsides to this approach to delegates:</p> <ul> <li>No type checks, save those which you do yourself by checking the 'fun_type' field.</li> <li>Type checks introduce extra conditionals into your code, making it messier and more branchy than before.</li> <li>The number of (safe) possible permutations of the function is limited by the size of the 'fun_type' variable.</li> <li>The enum and list of function pointer definitions would have to be machine generated. Anything else would border on insanity, save for trivial cases.</li> <li>Going through ordinary C, sadly, is not as efficient as, say a <em>mov -> call</em> sequence, which could probably be done in assembly (with some difficulty).</li> </ul> <p><br /></p> <h2>Does anyone know of a better way to do something like delegates in C?</h2> <p><strong>Note:</strong> The more portable and efficient, the better</p> <p><strong>Also, Note:</strong> I've heard of <a href="http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible" rel="nofollow noreferrer">Don Clugston's very fast delegates</a> for C++. However, I'm not interested in C++ solutions--just C .</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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