5 ms·
Metaprogramming custom control structures in C
- comex 12y agoSome C hacks have advantages in theory but hard to use effectively, like named+default parameters via struct. Others are practical, but only if you don't care about compilers without GNU extensions (i.e. MSVC), such as RAII via attribute cleanup and safer macros via statement expressions. This control structure trick, despite being more esoteric looking than any of those, is both useful in practice and standard C. If you're defining a custom data container in C, it will let you write a foreach macro that doesn't require the user to declare any temporaries you need to use, doesn't break the break and continue statements, and doesn't require any weird syntax at the beginning of end of the loop. I highly recommend reading about it. For a (poorly documented) example of actual use from some code I wrote, see HTAB_FOREACH: https://github.com/comex/substitute/blob/master/lib/cbit/htab.h https://github.com/comex/substitute/blob/master/lib/cbit/hta... and the LET_LOOP macro it uses: https://github.com/comex/substitute/blob/master/lib/cbit/misc.h https://github.com/comex/substitute/blob/master/lib/cbit/mis...
- nitrogen 12y agoI use named parameters in production C code for Nitrogen Logic. It just requires organizing things so that 0 is an acceptable default.
- pjlegato 12y agoStep 1: Write a Lisp interpreter in C. :)
- deleted 12y ago[deleted]
- edward 12y agoSimon Tatham is the author of putty, he knows what he is talking about.
- gosub 12y agoalso author of my favourite puzzle collection http://www.chiark.greenend.org.uk/~sgtatham/puzzles/ http://www.chiark.greenend.org.uk/~sgtatham/puzzles/