C ++宏有条件编译代码? [英] C++ Macro to conditionally compile code?
问题描述
我想根据宏来有条件地编译代码。基本上我有一个宏,看起来像(从真实版本简化):
#if DEBUG
问题是如果
#define START_BLOCK (x)if(DebugVar(#x)\
{char debugBuf [8192];
#define END_BLOCK()printf(%s\\\
,debugBuf);}
#else
#define START_BLOCK(x)(void)0;
#define END_BLOCK()(void)0;
#endif
DEBUG
被定义,你可以这样做:START_BLOCK(test)
char str [] =Test is defined;
strcpy(debugBuf,str);
END_BLOCK()
START_BLOCK(foo)
char str [] =Foo is defined;
strcpy(debugBuf,str);
END_BLOCK b $ b一切都正常,因为每个块都在自己的范围内,但如果DEBUG没有定义,你会得到一个重新定义
str
在第二个块。(你也会得到debugBuf
没有定义,这只是简化示例的一个副作用。)
我想做的是让#else像这样:
#else
#define START_BLOCK(x)#if 0
#define END_BLOCK()#endif
#endif
或者在开始/结束块之间没有任何东西的其他方法被编译。我试过上面的,我也尝试了一些类似的行:
#else
#define NULLMACRO 。)(void)0
#define START_BLOCK(x)NULLMACRO(
#define END_BLOCK())
#endif
没有运气。
有办法吗?我想到的只是我可能滥用优化编译器和使用:
#else
#定义START_BLOCK(x)if(0){
#define END_BLOCK()}
#endif
并相信它会完全编译块。
解决方案那么你想要有自己的范围的条件块吗?
这是一个相当可读的解决方案,依靠编译器来优化它:
#define DEBUG 1
if(DEBUG){
// ...
}
这里是一个只有预处理器的:
#define DEBUG 1
#ifdef DEBUG
#define IFDEBUG(x){x}
#else
#define IFDEBUG(x)
#endif
IFDEBUG b $ b // ...
)
或手动:
#define DEBUG 1
#ifdef DEBUG
{
// ...
}
#endif
I want to compile code conditionally based on a macro. Basically I have a macro that looks like (Simplified from the real version):
#if DEBUG #define START_BLOCK( x ) if(DebugVar(#x) \ { char debugBuf[8192]; #define END_BLOCK( ) printf("%s\n", debugBuf); } #else #define START_BLOCK( x ) (void)0; #define END_BLOCK( ) (void)0; #endif
The issue is that if
DEBUG
is defined you could do things like:START_BLOCK( test ) char str[] = "Test is defined"; strcpy(debugBuf, str); END_BLOCK( ) START_BLOCK( foo ) char str[] = "Foo is defined"; strcpy(debugBuf, str); END_BLOCK( )
And everything works fine because each block is within it's own scope. However if DEBUG isn't defined, then you'd get a redefinition of
str
in the second block. (Well you'd also getdebugBuf
not defined but that's just a side effect of the simplified example.)What I'd like to do is to have the #else be something like:
#else #define START_BLOCK( x ) #if 0 #define END_BLOCK( ) #endif #endif
Or some other method of not having anything between the start / end blocks be compiled. I tried the above, I also tried something along the lines of:
#else #define NULLMACRO( ... ) (void)0 #define START_BLOCK( x ) NULLMACRO( #define END_BLOCK( ) ) #endif
without any luck.
Is there a way for this to work? One thought that just occurred to me is that I could maybe abuse the optimizing compiler and use:
#else #define START_BLOCK( x ) if(0){ #define END_BLOCK( ) } #endif
And trust that it will just compile out the block completely. Are there any other solutions?
解决方案So you want conditional blocks with their own scope?
Here's a quite readable solution that relies on the compiler to optimize it away:
#define DEBUG 1 if (DEBUG) { // ... }
And here is one that is preprocessor-only:
#define DEBUG 1 #ifdef DEBUG #define IFDEBUG(x) {x} #else #define IFDEBUG(x) #endif IFDEBUG( // ... )
Or manually:
#define DEBUG 1 #ifdef DEBUG { // ... } #endif
这篇关于C ++宏有条件编译代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!