I am trying to create a macro that executes blocks of code only if it’s a debug build. I’ve managed to make one that executes one line only if debug is enabled, but i cannot figure out how to do a whole block of code.
the one line macro is below:
#include <iostream>
//error checking
#if defined(DEBUG) | defined(_DEBUG)
#ifndef DBG_ONLY
#define DBG_ONLY(x) (x)
#endif
#else
#ifndef DBG_ONLY
#define DBG_ONLY(x)
#endif
#endif
int main () {
DBG_ONLY(std::cout << "yar" << std::endl);
return 0;
}
Wrap the macro inside a
do-whileloop so that you avoid problems when using your macro in conditional statements such asif (cond) DBG_ONLY(i++; j--;). It also creates a new scope for debug only declarations:This will fail if you have statements like
int i,j. For that, we need a variadic macro I guess: