Macro to substitute the #ifdef DEBUG #endif pair

Started by
3 comments, last by wqking 12 years, 8 months ago
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?
Advertisement
I think you will have to surround the content you pass to the macro inside parenthesis.
DBG((, var2(param) ))

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..


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.

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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement