# Macro to substitute the #ifdef DEBUG #endif pair

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a way to create a macro to accept anything as parameter, to avoid a bunch of ugly #ifdef DEBUG #endif?
I tried:

 #ifdef _DEBUG #define DBG(param) param #else #define DBG(param) #endif

But I cant use it, i.e., to initialize a debug only member in a initialization list:

 myClass::myClass():var1(0) DBG(, var2(param) ) {..}

because of the comma, "param" is ignored..

Can I create a macro that just copy and paste everything?

##### Share on other sites
I think you will have to surround the content you pass to the macro inside parenthesis.
DBG((, var2(param) ))

##### Share on other sites

I think you will have to surround the content you pass to the macro inside parenthesis.
DBG((, var2(param) ))

This doesnt work, cause the parenthesis goes to the initialization list, its not discarded..

##### Share on other sites
First declare a macro for the comma and use that instead of the comma
#define COMMA , ... DBG(COMMA var2(param) ) ...
I know, it is not very nice. I guess it's just easier to use #ifdef and #endif.

##### Share on other sites

First declare a macro for the comma and use that instead of the comma
#define COMMA , ... DBG(COMMA var2(param) ) ...
I know, it is not very nice. I guess it's just easier to use #ifdef and #endif.

That works on VC, also works on GCC for simple situation.
For complex macro, it can't compile.

Use this,
 #define COMMA() , ... DBG(COMMA() var2(param) ) ... 
will work well.

However, on GCC, if COMMA() is passed to macro A, then A pass it to another macro, it will fail compile too.
So always using COMMA() as first level parameter.
VC doesn't have that limitation.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631714
• Total Posts
3001861
×