Templating in C
"Nevertheless, this is still a few order of magnitude better than C++ templating."
Uh... no. On a personal level, I have always found that almost any type of preprocessor magic, especially re-#including with a different set of parameters in effect is a recipe for disaster.
Any use of the pre-processor seems to have a serious impact on the build-system (tracking parallel variants), the IDE (color, intellisense or auto-completion), the debugger, and apparently any tool that operates at the meta-level.
On the other hand, IDEs and debuggers often also have problems with C++ templates as well. Am I crazy to think that the result has generally been better with templates?
I think we would better off without any of this. We should: - discard the preprocessor, - expanding and tightening control on templating), and - migrating towards more conventional module import methods, such as http://clang.llvm.org/docs/Modules.html (instead of #include, unless you can fix IWYU).
Curious it doesn't mention C11's __Generic macro. A very nice way to achieve a lot of useful "overloading" behavior.
Just use C++. The lack of semantically aware, type safe templates is one of the deficiencies of C that motivated the development of C++.
C++ is a strictly more powerful language than C. Play to its strengths rather than abusing C's weaker constructs.
Another solution, which seems to be missing is to use function pointers; process_image could receive a pointer to given function instead of n. My experiences with gcc show, that functions passed by pointer are often inlined, which is not really a surprise, since they are constant arguments of the function.
Here is an example [0], although the wrapper functions seem to be redundant in this case.
Why complicate with multiple files, functions, defines and includes; here it is in one file with a single define:
We can improve the macro further by removing the unnecessary if statement and the index number:
I did something similar in a personal project[0], but to avoid reimplementing things like lists and resizable buffers and such over and over again but maintain a sort of "type safety.*" It's a step down from C++ templates but it works.
I am looking for a book or web page which describes well programming patterns in C (might be focused on OOP in C). I mostly design for embedded systems. Any recomendations?
here's a similar technique I use in lthread to generate functions:
https://github.com/halayli/lthread/blob/master/src/lthread_s...
the arguments are complete function signature
The most interesting code is dark grey text on black background.
C macros are powerful.
undefined
Very interesting!