Known Problems of the C/C++ Preprocessor

Preprocessor metaprogramming is subject to heated discussions.  Part of this is caused by bad experiences with dangerous techniques, such as defining inline functions using macros.  As a rule of thumb, if you can find a clean and manageable way to do something without the preprocessor, then you should do it that way.
Let's survey some of the widely known problems of the preprocessor in a problem/solution format.

Problem #1

The preprocessor does not respect scope, therefore macros can accidentally and sometimes silently replace code.
Solution A
Use all caps identifiers for macros and only macros.  This practically eliminates the possibility that a macro might replace other kinds of code accidentally.
Solution B
Use the local macro idiom:
#define MACRO ...
// use MACRO
#undef MACRO
This makes sure that a macro cannot accidentally replace code outside of the scope of the local macro.
A problem with this solution is that the #undef cannot be automated and may be forgotten.  Experienced programmers generally write the #undef either immediately before (in time) or immediately after writing the macro definition.
Solution C
Use the unique macro prefix idiom.
#define UMP_MACRO
// use UMP_MACRO
This makes accidental substitution and collisions highly unlikely.  Problems with this solution include:
By combining all solutions, whenever possible, the scope problem can be largely avoided.

Problem #2

Preprocessor code is difficult to read.  It requires an understanding of the basic process of how the preprocessor recursively expands macros, finding macro definitions, and mentally substituting the parameters of the macro.
Solution
Any kind of programming requires a basic understanding of how the code is executed.  Any parameterization technique, including simple functions and templates requires finding the definition and mentally substituting parameters.
However, it is good to know a few techniques:
An especially important thing to remember is to limit the use of the preprocessor to structured, well-understood, and safe methods.  Structure helps to understand complex systems [McConnell].

Problem #3

"I'd like to see Cpp abolished." - Bjarne Stroustrup in [Stroustrup].
Solution
The C/C++ preprocessor will be here for a long time.
In practice, preprocessor metaprogramming is far simpler and more portable than template metaprogramming [Czarnecki].

© Copyright Housemarque Oy 2002
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies.  This document is provided "as is" without express or implied warranty and with no claim as to its suitability for any purpose.

© Copyright Housemarque Oy 2002
© Copyright Paul Mensonides 2002

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)