Chapter 34. Preprocessor Macros
Difficulty: 4
Preprocessor macros are part of C++'s heritage from C. This Item considers whether they are still relevant in the modern C++ scheme of things.
With flexible features such as overloading and type-safe templates available, why would a C++ programmer ever write a preprocessor directive like “#define
”?
Solution
C++ features often, but not always, cancel out the need for using #define
. For example, “const int c = 42
;” is superior to “#define c 42
” because it provides type safety, and avoids accidental preprocessor edits, among other reasons.
There are, however, still a few good reasons to write #define
.
1. Header ...
Get More Exceptional C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.