###  | # Boost.Preprocessor ## Widely known problems with the 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 using the preprocessor, then you should do it that way.

Let's survey some of the widely known problems with the preprocessor in a problem/solution format.

PROBLEM: 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 kind of code accidentally.

SOLUTION B: Use the Local Macro idiom:

>     #define MACRO ...
>     // Use MACRO
>     #undef MACRO

This makes sure that a macro can not accidentally replace code outside of the scope of the local macro.

A problem with this solution is that the #undef can not 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:

- There can still be naming collisions inside a large project.
- Macros still pollute the global namespace.

By combining all solutions, whenever possible, the scope problem can be largely avoided.

PROBLEM: Preprocessor code is difficult to read. It requires understanding the basic process of how the preprocessor recursively expands macros, finding the macro definition and mentally substituting the parameters of the macro.

SOLUTION: Any kind of programming requires 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:

- By using as many Local Macros as reasonable, the bulk of the searching process can be eliminated.
- Code browsers and text search tools make it easier to find the definitions.
- The compiler can be used for generating the preprocessed source code in order to look for bugs.
- Before turning something into a preprocessor metaprogram, first implement a small scale version of it without preprocessor. Then work bottom->up replacing hand written constructs by using preprocessor. This way you can test the code incrementally. Experienced programmers often skip many stages, but if something proves too complex to write directly, it is always possible to fall back to incremental methods.
- If you insert a special symbol into the preprocessor code in places where there should be a line break, you can make code readable after preprocessing simply by using a search and replace tool.

An especially important thing to remember is to limit the use of preprocessor to the structured, well understood and safe methods. Structure helps to understand complex systems [McConnell].

PROBLEM: "I'd like to see Cpp abolished." - Bjarne Stroustrup in [Stroustrup]

SOLUTION: The C preprocessor will be here for a long time.

In practice, preprocessor metaprogramming is far simpler and more portable than template metaprogramming [Czarnecki].

Revised

© 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.