I have encountered the #define pre-processor directive before while learning C, and then also encountered it in some code I read. But apart from using it to definite substitutions for constants and to define macros, I’ve not really understook the special case where it is used without a “body” or token-string.
Take for example this line:
#define OCSTR(X)
Just like that! What could be the use of this or better, when is this use of #define necessary?
This is used in two cases. The first and most frequent involves
conditional compilation:
You’ve surely used this yourself for include guards, but it can also be
used for things like system dependencies:
(In this case, WIN32 is more likely defined on the command line, but it
could also be defined in a
"config.hpp"file.) This would normallyonly involve object-like macros (without an argument list or
parentheses).
The second would be a result of conditional compilation. Something
like:
That allows writing things like:
which will call the function if
DEBUGis defined, and do nothing if itisn’t.