测试空宏定义

发布于 2024-09-30 21:16:12 字数 596 浏览 1 评论 0原文

我在tracing.hh 中有一组调试宏。是否生成代码并输出是由真实源代码中的宏标志控制的:

// File:  foo.cc
#define TRACING 0
#include "tracing.hh"
// Away we go . . .
TRACEF("debug message");

标志TRACING应该有一个值;我通常在 0 和 1 之间切换。

在tracing.h 中,

  • #ifdef TRACING 会告诉我跟踪已定义。
  • #if TRACING 控制诸如 TRACEF() 之类的函数宏的定义

,但是如果 TRACING 没有值怎么办?然后#if TRACING 会产生错误:

In file included from foo.c:3:
tracing.hh:73:12: error: #if with no expression

How can I test if TRACING is Defined but has no value?

I've got a set of debug macros in tracing.hh. Whether it generates code and output is controlled by a macro flag in the real source code:

// File:  foo.cc
#define TRACING 0
#include "tracing.hh"
// Away we go . . .
TRACEF("debug message");

The flag TRACING should have a value; I usually toggle between 0 and 1.

Within tracing.h,

  • #ifdef TRACING will tell me that tracing was defined.
  • #if TRACING controls the definition of functional macros like TRACEF()

But what if TRACING has no value? Then #if TRACING produces an error:

In file included from foo.c:3:
tracing.hh:73:12: error: #if with no expression

How can I test if TRACING is defined but has no value?

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

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

发布评论

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

评论(3

仙女 2024-10-07 21:16:12

根据 Matti 的建议和更多的探索,我认为问题是这样的:如果 TRACING 没有值,我们需要在测试 #if ... 中使用有效的预处理器表达式。 Gnu cpp 手册
说它必须计算为整数表达式,因此我们需要一个即使缺少其中一个参数也有效的表达式。我最后想到的是:

#if (TRACING + 0)
#  . . .
  • 如果 TRACING 有一个数值(如 #define TRACING 2 \n),cpp 有一个有效的表达式,并且我们没有改变值。
  • 如果 TRACING 没有值(如 #define TRACING \n),预处理器将 #if (+0) 计算为 false

唯一不处理的情况是

  • 如果 TRACING 具有非数字值(ieON)。 cpp 手册说“非宏的标识符...都被认为是数字零,”,其计算结果为false。然而,在这种情况下,将其视为 true 值更有意义。唯一正确的做法是布尔文字 truefalse

With Matti's suggestion and some more poking, I think the issue is this: if TRACING has no value, we need a valid preprocessor expression in the test #if .... The Gnu cpp manual
says it has to evaluate to an integer expression, so we need an expression that is valid even if one of the arguments is missing. What I finally hit on is:

#if (TRACING + 0)
#  . . .
  • If TRACING has a numerical value (as in #define TRACING 2 \n), cpp has a valid expression, and we haven't changed the value.
  • If TRACING has no value (as in #define TRACING \n), the preprocessor evaluates #if (+0) to false

The only case this doesn't handle is

  • If TRACING has a non-numerical value (i.e., ON). The cpp manual says "Identifiers that are not macros . . . are all considered to be the number zero," which evaluates to false. In this case, however, it would make more sense to consider this a true value. The only ones that do the right thing are the boolean literals true and false.
夜夜流光相皎洁 2024-10-07 21:16:12

虽然迟到了,但我发现了一个很好的技巧来区分

#define TRACING 0

#define TRACING

如果

#if (0-TRACING-1)==1 && (TRACING-0)!=-2
#error "tracing empty"
#endif

TRACING 为空,则表达式的计算结果为 0--1 => <代码>1。

如果 TRACING 为 0,则表达式的计算结果为 0-0-1 => -1

我在 TRACING==-2 情况下添加了额外的检查,这将使第一个测试通过。

当然,这不适用于字符串文字。

Late to the party but I found a nice trick to distinguish

#define TRACING 0

from

#define TRACING

like this:

#if (0-TRACING-1)==1 && (TRACING-0)!=-2
#error "tracing empty"
#endif

If TRACING is empty, the expression evaluates to 0--1 => 1.

If TRACING is 0, the expression evaluates to 0-0-1 => -1

I added an extra check in case TRACING==-2 which would make the first test pass.

This doesn't work for string literals of course.

做个ˇ局外人 2024-10-07 21:16:12

#if defined(TRACING) && !TRACING

成功吗?

Would

#if defined(TRACING) && !TRACING

do the trick?

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