Tuesday, August 19, 2003

The joy of macros

C and C++ is two of my favorite languages, but the macro preprocessor can be unwieldy at times. Consider this:

#define STR_1(line)   #line
#define STR_2(line)   STR_1(line)
#define malloc(n)      \ 
    (debug_malloc(       \ 
         (n), __FILE__ ":" STR_2(__LINE__)))

What is this? It's the only way to convert the current line number into a string usable in program diagnostics: you have to use the double macro call to make it work. Nasty, isn't it? I'm glad Jon Jagger wrote about that so I wouldn't have to.

Post a Comment