4 ms·
In actual production use, I think it could be better to write a separate program that will generate the code for you instead of using the C preprocessor (or use
by ncraun 11y ago
In actual production use, I think it could be better to write a separate program that will generate the code for you instead of using the C preprocessor (or use a language with this kind of introspection built in). However, using this hack lets you depend only on the facilities of the language, and that can be useful in simplifying the build process. Plus it was fun to create, too!
- uxcn 11y agoIt can be a bit tricky if it isn't integrated with the build process.
- Kristine1975 11y agoYou can use e.g. cog which puts the generated code into the file itself: http://nedbatchelder.com/code/cog/ http://nedbatchelder.com/code/cog/ Then all you need to do is to remember to run the preprocessor after you've made a change to the generator code.
- uxcn 11y agoPython is definitely a better syntax to write meta-programs than the pre-processor, but you still have the issue of the additional step/tool. Anything that depends on the generated code depends on the step which, if it's missed, can lead to non-good things.
- Kristine1975 11y agoSure. But you do test your changes, don't you? ;-) So if you forget to run cog after making a change to the meta-program, you should notice right away. But it still is a cludge. It would be better if C/C++ had a better preprocessor.
- uxcn 11y agoIt isn't a question of testing changes. The more people working on the codebase, the more people it impacts. Adding manual steps is tempting the fates.