TL;DR<p>Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don't want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like:<p><pre><code> #include "foo_defs.h"
#define BAR 42
FOO;
BAR;
</code></pre>
We filter out the include line and then preprocess the file, getting the following output:<p><pre><code> FOO;
42;</code></pre>
My uses of macros in C++ these days boil down to a few common use cases that are still insanely not handled well by the language:<p>0. Anything involving file and line number.<p>1. Generating a string version of something alongside the value of something, without repeating myself.<p>2. Creating printing macros to keep me from typing out the entire asinine syntax for something like “std::cerr << foo << bar << baz << std::endl” so I only need PRINT(foo << bar << baz). Substituting code fragments is dead simple with a macro and absurdly complex or impossible with other C++ mechanisms. Ironically I’m only doing it because of the poor design of the entire “iostream” stack.
Relatedly, with all the clang-based tooling like clang-tidy or clang-format, it's kind of disappointing that there isn't a tool to expand arbitrary macros in a source.<p>That would have been extremely useful when I was recently refactoring some old C code. I ended up writing a small script that can expand one macro at a time, but I wish I hadn't had to.<p>I'm sure that script doesn't work in the general case.
I try to use ObjC PPC macros for things that are constant for the run of the program, and can be reasoned about globally.<p>For example, some good ones I use are isLandscape or isIPad, which are both variable depending on the user, but constant for the run of the app. Perfect for a PCH file I think.