Aussie AI
Self-test Code Block Macro
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Self-test Code Block Macro
An alternative formulation of a macro for installing self-testing code using a block-style, rather than a function-like macro, is as follows:
SELFTEST { // block of debug or self-test statements }
The definition of the SELFTEST
macro looks like:
#if YDEBUG #define SELFTEST // nothing (enables!) #else #define SELFTEST if(1) {} else // disabled #endif
This method relies on the C++ optimizer to fix the non-debug version,
by noticing that “if(1)
” invalidates the else
clause,
so as to remove the block of unreachable self-testing code that's not ever executed.
Note also that SELFTEST
is not function-like, so we don't have the “forgotten semicolon” risk
when removing SELFTEST
as “nothing”.
In fact, the nothing version is actually when SELFTEST
code is enabled,
which is the opposite situation of that earlier problem.
Furthermore, we cannot use the “do-while(0)
” trick in this different syntax formulation.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |