Test for empty macro definition
I've got a set of debug macros in tracing.hh. Whether it generates code and output is controlled by a macro flag in the real source code: // File: foo.cc #define TRACING 0 #include "tracing.hh" // Away we go . . . TRACEF("debug message"); The flag TRACING should have a value; I usually toggle between 0 and 1. Within tracing.h, #ifdef TRACING will tell me that tracing was defined. #if TRACING controls the definition of functional macros like TRACEF() But what if TRACING has no value? Then #if TRACING produces an error: In file included from foo.c:3: tracing.hh:73:12: error: #if with no expression How can I test if TRACING is defined but has no value?
With Matti's suggestion and some more poking, I think the issue is this: if TRACING has no value, we need a valid preprocessor expression in the test #if .... The Gnu cpp manual says it has to evaluate to an integer expression, so we need an expression that is valid even if one of the arguments is missing. What I finally hit on is: #if (TRACING + 0) # . . . If TRACING has a numerical value (as in #define TRACING 2 \n), cpp has a valid expression, and we haven't changed the value. If TRACING has no value (as in #define TRACING \n), the preprocessor evaluates #if (+0) to false The only case this doesn't handle is If TRACING has a token value (i.e., ON). The cpp manual says "Identifiers that are not macros . . . are all considered to be the number zero," which evaluates to false. In this case, however, it would make more sense to consider this a true value. The only tokens that do the right thing are the boolean literals true and false.
Would #if defined(TRACING) && !TRACING do the trick?
Compiling with ocamlbuild and camlp5
How not to output comments using the -C operation in mcpp
C++ Builder File Version not correct
How do you a preprocess statement for #include
Does the preprocessor pass environment variables?
YAML preprocessor / macro processor
Pre-Processing using m4
Is there a practical reason for “#if defined(X) && (X != 0)”?
How to check for presence of a directory in Inno Setup preprocessor?
What are analogs of “#ifdef”, “#ifndef”, “#else”, “#elif”, “#define”, “#undef” in D programming lnaguage?
Image pre-processing in OCR
which is more important, number of variables or subexpressions?
What is the purpose of using #define to define a constant with no value?
Name of file during compile time in Progress 4gl
Triple conditional check with ifdef in M4
How to process map data from OpenStreetMap with Java?