4 ms·
A better pattern instead of declaring via a macro that expands to a large number of functions is to define the implementation in a file, require the user to #de
by david2ndaccount 6y ago
A better pattern instead of declaring via a macro that expands to a large number of functions is to define the implementation in a file, require the user to #define STACK_T and then #include the file. At the bottom of the file, #undef STACK_T. As files can be included multiple times, you can do this for each generic structure you like. A sort of poor-man’s templates, that you can reasonably step through in a debugger.
- Galanwe 6y agoNote that include guards become tricky then.
- deleted 6y ago[deleted]
- TheNewAndy 6y agoYou don't use include guards with this pattern. See https://en.wikipedia.org/wiki/X_Macro https://en.wikipedia.org/wiki/X_Macro for more.
- ludocode 6y agoYes! The include style of templates in C is way better than the old way of huge macros to instantiate code. The template code can look mostly like idiomatic C, it interacts way better with a debugger, it gives better compiler errors... everything about it is better and it's finally starting to become more popular. I've open sourced my own C template library here: https://github.com/ludocode/pottery https://github.com/ludocode/pottery Not only does it use the #include style of templates, but it actually makes the templates composable. It takes this idea pretty far, for example having a lifecycle template that lets you define operations on your type like move, copy, destroy, etc. This way the containers can fully manage the lifecycles of your types even if they're not bitwise movable. There's also this other more popular C template library, one that tries to more directly port C++ templates to C but with a lot less features: https://github.com/glouw/ctl/ https://github.com/glouw/ctl/