Can I define a macro that defines macros?

The question came to mind after looking at the memory leak detection mechanism in VS. The following template code is needed there:

#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>\ 

impossible.

Are there any workarounds - suggestions?

+6
source share
2 answers

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

6.10.3.4 Re-scanning and further replacement

[...]

The resulting fully macro-replaceable sequence of preprocessing tokens is not processed as a preprocessing directive, even if it looks like one, [...]

(This “even if it looks like one” remark is completely redundant, because, of course, no syntax is considered a preprocessing directive if it does not look like one! However, words have been inserted into your mind for many years. I just found a quote section by searching for a document so that the string “looks like one.”)

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

I usually do such things.

+6
source

Source: https://habr.com/ru/post/972470/


All Articles