C++有条件编译代码的宏?

发布于 2024-12-03 22:21:22 字数 1214 浏览 0 评论 0原文

我想根据宏有条件地编译代码。基本上我有一个看起来像(从真实版本简化)的宏:

#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

问题是,如果定义了 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( )

一切正常,因为每个块都在其自己的范围内。但是,如果未定义 DEBUG,那么您将在第二个块中重新定义 str。 (好吧,您还会得到 debugBuf not Defined ,但这只是简化示例的副作用。)

我想做的是让 #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
    #define START_BLOCK( x ) if(0){
    #define END_BLOCK( ) }
#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 get debugBuf 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月依秋水 2024-12-10 22:21:22

那么您想要具有自己范围的条件块吗?

这是一个非常可读的解决方案,它依赖于编译器来优化它:

#define DEBUG 1

if (DEBUG) {
    // ...
}

这是一个仅预处理器的解决方案:

#define DEBUG 1

#ifdef DEBUG
    #define IFDEBUG(x) {x}
#else
    #define IFDEBUG(x)
#endif

IFDEBUG(
    // ...
)

或者手动:

#define DEBUG 1

#ifdef DEBUG
{
    // ...
}
#endif

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
爱给你人给你 2024-12-10 22:21:22

会:

#if DEBUG
    #define START_BLOCK( x ) if(DebugVar(#x) \
        { char debugBuf[8192];
    #define END_BLOCK( ) printf("%s\n", debugBuf); }
#else
    #define START_BLOCK( x ) {
    #define END_BLOCK( ) }
#endif

会吗?

Would:

#if DEBUG
    #define START_BLOCK( x ) if(DebugVar(#x) \
        { char debugBuf[8192];
    #define END_BLOCK( ) printf("%s\n", debugBuf); }
#else
    #define START_BLOCK( x ) {
    #define END_BLOCK( ) }
#endif

do?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文