Sign in to follow this  
Icebone1000

Macro to substitute the #ifdef DEBUG #endif pair

Recommended Posts

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

[code]
#ifdef _DEBUG
#define DBG(param) param
#else
#define DBG(param)
#endif[/code]


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

[code]

myClass::myClass():var1(0) DBG(, var2(param) )
{..}[/code]

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

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

Share this post


Link to post
Share on other sites
Wooh    1088
I think you will have to surround the content you pass to the macro inside parenthesis.
[code]DBG((, var2(param) ))[/code]

Share this post


Link to post
Share on other sites
Icebone1000    1958
[quote name='Wooh' timestamp='1312749443' post='4845886']
I think you will have to surround the content you pass to the macro inside parenthesis.
[code]DBG((, var2(param) ))[/code]
[/quote]

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


Share this post


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

Share this post


Link to post
Share on other sites
wqking    761
[quote name='Wooh' timestamp='1312750998' post='4845903']
First declare a macro for the comma and use that instead of the comma
[code]#define COMMA ,
... DBG(COMMA var2(param) ) ...[/code]
I know, it is not very nice. I guess it's just easier to use #ifdef and #endif.
[/quote]

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

Use this,
[code]
#define COMMA() ,
... DBG(COMMA() var2(param) ) ...
[/code]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this