3 ms·
No one likes the preprocessor, but it's practicaly required if you don't want to move to C++ with template metaprogramming.
by lupire 5y ago
No one likes the preprocessor, but it's practicaly required if you don't want to move to C++ with template metaprogramming.
- marcosdumay 5y agoWell, C++ templates happen on the preprocessor too.
- adrianN 5y agoNo they don't. Template instantiation happens during compilation, way later than the preprocessor.
- kllrnohj 5y agoNot really? They're part of compilation and obey all the type rules & syntax of the language. They're not textual replacements that run in a distinctly different phase like macros are.
- jcelerier 5y agowhere in hell did you learn that, this is entirely false
- __d 5y agoWell, it is today. When I first learned C++, using cfront in the late 80's, lots of the language was implemented as C pre-processor macros.
- jcelerier 5y agomaybe lots of the language, but not templates. Cfront 2 did not have templates, and Cfront 3 did have them without using the preprocessor. You can even check out how it was done: https://github.com/seyko2/cfront-3/blob/master/src/template.C https://github.com/seyko2/cfront-3/blob/master/src/template....
- badsectoracula 5y agoI do like the preprocessor and my major pet peeve with C is that the preprocessor has been stagnant for ages. Like, why the hell can't i do something like #define BASE hey #define HEADERS foo bar baz #append HEADERS bad bal bah #push OSSUFFIX #ifdef WIN32 #define OSSUFFIX win32 #else #define OSSUFFIX unknown #endif #foreach H HEADERS #eval include "$(BASE)/$(OSSUFFIX)/$(HEADERS)" #endfor #pop OSSUFFIX People go to great lengths making all sorts of weird structures from x-macros, repeated statements, defines that only exist to be used by other defines, etc all to work around existing preprocessor limitations - and many of them would simply become unnecessary if the preprocessor could do things like variable editing, loops and being able to eval its own commands. Even though some stuff can be done via language features, it is often necessary and more flexible to work with the source code itself.
- midjji 5y agoOne alternative is to just write a C code generator, which lets you mix C and some sensible language, eg. python. Then use that to generate the code which is then sent to gcc.