DEV Community

Discussion on: The Coolest Programming Language Features

Collapse
 
darkain profile image
Vincent Milum Jr

The C/C++ preprocessor is still a feature I miss in virtually every other language I ever touch. The ability to transmute the actual code before it is parsed is invaluable in those environments. It also ties in directly with the inline ASM on your list, where the preprocessor can be used to select the proper ASM architecture for the given platform.

A great example of this is used in crypto libraries. They generally have a basic C/C++ implementation, but also optimized versions of certain routines for x86, x86-64, AES-NI, ARM, ARM-NEON, and more.

It is also nice to have platform-specific or build-specific flags that can toggle various parts of code on/off, or change things up entirely. This helps when porting code for example, because the path to a particular file may not be the same between Linux, BSD, MacOS, and Windows.

One last trick to mention. The preprocessor can also modify the linker. In one of my projects, I have preprocessor directives to link in static libraries. These libraries are compiler specific, so instead of creating countless different build scripts, I have a single CPP file with them all listed with different #ifdef tags to target each compiler.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Very cool stuff! I try to stay away from languages like C/C++ because I don’t love managing my own memory. That said, I’m always surprised with how powerful those languages are.

Collapse
 
darkain profile image
Vincent Milum Jr

Self-managed memory does add some issues, but it also affords some great advantages, too! Last year I worked on optimizing a web server for a micro controller that only has 80KiB of RAM so it required a lot of custom fine-tuning of memory management to get the thing to perform well.

Here is an example of one method I converted from a C++ managed memory library to some unmanaged code to help reduce the RAM consumption:
gist.github.com/darkain/23da34f00e...

Collapse
 
phlash profile image
Phil Ashby

Given that the C pre-processor can usually be invoked without the language compiler (eg: gcc -E), it can and indeed has been used to pre-process other languages, Wikipedia has a nice list of abuse at the end here:
en.wikipedia.org/wiki/Preprocessor

Of course many people think pre-processing is bad (mostly I agree):
stackoverflow.com/questions/321791...
literateprogramming.com/ifdefs.pdf