header image
C++ Marco Debug Options
January 26th, 2012 under Programming. [ Comments: none ]

To be honest, it should be straight forward but I took me a while to figure it out.

I wanted to mark certain lines in the code such that I can easily enable/disable it. So here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
// set to 1 to enable timing and output
#if 0 
   #define TIMING 
   #define OUTPUT 
#else
   #define TIMING / ## /
   #define OUTPUT / ## /
#endif
...
   TIMING startTimer();
   ...
   TIMING stopTimer();
...
// set to 1 to enable timing and output
#if 0 
   #define TIMING 
   #define OUTPUT 
#else
   #define TIMING / ## /
   #define OUTPUT / ## /
#endif
...
   TIMING startTimer();
   ...
   TIMING stopTimer();
...

when

1
#define TIMING / ## /
#define TIMING / ## /
is used then is replaced with . Thus the entire line is commented and therefore disabled. This way i can easily turn on/off several debugging facilities.

The

in the macro definition means that the previous and successive pieces of text are concatenated. So
1
TIMING myCode;
TIMING myCode;
results in
1
// myCode;
// myCode;
. We cannot use
1
#define TIMING //
#define TIMING //
because the won’t be parsed as the replacement string, it will be parsed as the comment literal.

This is a simple but handy technique.


C++ Map
January 17th, 2012 under Programming. [ Comments: none ]

Amazing work that shows C++ features and evolution of C++