Hacker News new | ask | show | jobs
by Stenzel 3329 days ago
I am missing the motivation to purge the preprocessor - only reason he gives is the paradigm that the preprocessor is considered bad practice. I fail to see why. The preprocessor is a useful tool that can save you time and make code more readable and portable. Conditions at compile time guarantee that the compiler is not subjected to a specific portion of code, and it offers the only way to define a constant value without having an implicit type for that. Considering the use of C/C++ outside pure computer programming, like embedded systems or digital signal processing, where portability, code size and target specific substitutions or optimisations matter, there is simply no reason and no way to get rid of the preprocessor without breaking things. Sure you can use it in obscure and wrong ways, but it is not the job of the language to act as a nanny and prevent you from doing evil by restricting access to dangerous toys.
3 comments

Author here, I didn't intend to give a motivation for purging it, many people in the C++ community want to and I wanted to explore the feasibility of that in the current status.

> like embedded systems or digital signal processing, where portability, code size and target specific substitutions or optimisations matter, there is simply no reason and no way to get rid of the preprocessor without breaking things.

It certainly is, I know embedded projects where they use template meta programming to get an even more advanced code generator.

The post seems to be more about evaluating alternatives to preprocessor-based solutions to common problems; even when alternatives exist, reasonable people can disagree about whether they're actually better, but I think most would agree that it would be nice to have a proper module system instead of using textual includes, or that the fact that macros cannot be namespaced and can lead to ODR violations is not awesome.

Anyway, the post ends saying

> With current C++(17), most of the preprocessor use can’t be replaced easily.

and I think we can all agree on that.

CPP is not without warts. It complicates static analysis (machine and human), for one thing. You need the definition of variable foo or function bar? Great, which one? Who made the last assignment to foo and what its value could be? Does this ifdefed snippet here count?
It is also not without very legitimate uses. Feel free to not use the preprocessor if you don't want (you're still using it with `#include` though), but it is very useful for specific use cases, and there is no real reason to remove support for it.