“#ifdef”在宏内部

发布于 2024-12-01 21:53:44 字数 360 浏览 0 评论 0原文

可能的重复:
#define 内的#ifdef

如何在宏中成功使用字符“#”?当我做类似的事情时它会尖叫:

#define DO(WHAT)        \
#ifdef DEBUG        \                           
  MyObj->WHAT()         \       
#endif              \

Possible Duplicate:
#ifdef inside #define

How do I use the character "#" successfully inside a Macro? It screams when I do something like that:

#define DO(WHAT)        \
#ifdef DEBUG        \                           
  MyObj->WHAT()         \       
#endif              \

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

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

发布评论

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

评论(5

未蓝澄海的烟 2024-12-08 21:53:44

你不能那样做。你必须这样做:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT) do { } while(0)
#endif

do { } while(0) 避免空语句。例如,请参阅此问题

You can't do that. You have to do something like this:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT) do { } while(0)
#endif

The do { } while(0) avoids empty statements. See this question, for example.

悲念泪 2024-12-08 21:53:44

它尖叫是因为你不能这样做。

我建议采用以下替代方案:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT)
#endif

It screams because you can't do that.

I suggest the following as an alternative:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT)
#endif
岁吢 2024-12-08 21:53:44

看来您想要做的事情可以这样实现,而不会遇到任何问题:

#ifdef DEBUG
#    define DO(WHAT) MyObj->WHAT()
#else
#    define DO(WHAT) while(false)
#endif

顺便说一句,最好使用 NDEBUG 宏,除非您有更具体的理由不这样做。 NDEBUG 更广泛地用作表示不调试的宏。例如,可以禁用标准 assert 宏通过定义NDEBUG。你的代码将变成:

#ifndef NDEBUG
#    define DO(WHAT) MyObj->WHAT()
#else
#    define DO(WHAT) while(false)
#endif

It seems that what you want to do can be achieved like this, without running into any problems:

#ifdef DEBUG
#    define DO(WHAT) MyObj->WHAT()
#else
#    define DO(WHAT) while(false)
#endif

Btw, better use the NDEBUG macro, unless you have a more specific reason not to. NDEBUG is more widely used as a macro that means no-debugging. For example the standard assert macro can be disabled by defining NDEBUG. Your code would become:

#ifndef NDEBUG
#    define DO(WHAT) MyObj->WHAT()
#else
#    define DO(WHAT) while(false)
#endif
我是男神闪亮亮 2024-12-08 21:53:44

你可以像这样做同样的事情:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT)
#endif

You can do the same thing like this:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT)
#endif
长梦不多时 2024-12-08 21:53:44

怎么样:

#ifdef DEBUG
#define DO(WHAT) MyObj->WHAT()
#else
#define DO(WHAT)
#endif

How about:

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