Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define a macro that defines macros ?

Tags:

c++

macros

The question came to mind by looking at the memory leak detection mechanism in VS. There the following boilerplate code is needed :

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

but replacing this code with DETECT_MLEAKS :

#define DETECT_MLEAKS\
#define _CRTDBG_MAP_ALLOC\
#include <stdlib.h>\
#include <crtdbg.h>\

can't be done.

Are there any workarounds - suggestions ?

like image 495
Nikos Athanasiou Avatar asked Jul 18 '14 18:07

Nikos Athanasiou


2 Answers

//#define DETECT_MLEAKS //Uncomment to detect mem-leaks
#ifdef DETECT_MLEAKS
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

Is how I typically do such things.

like image 97
IdeaHat Avatar answered Nov 12 '22 00:11

IdeaHat


In the standard C language, macros cannot generate preprocessing directives. For instance in the ISO 9899:1999 standard:

6.10.3.4 Rescanning and further replacement

[ ... ]

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, [ ... ]

(This "even if it resembles one" remark is completely superfluous because, of course, no syntax is ever treated as a preprocessing directive if it does not resemble one! Yet, the words stick in your mind for years. I just located the quote's section by searching the document for the string "resembles one".)

like image 35
Kaz Avatar answered Nov 12 '22 00:11

Kaz