测试空宏定义
我在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 likeTRACEF()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Matti 的建议和更多的探索,我认为问题是这样的:如果
TRACING
没有值,我们需要在测试#if ...
中使用有效的预处理器表达式。 Gnu cpp 手册说它必须计算为整数表达式,因此我们需要一个即使缺少其中一个参数也有效的表达式。我最后想到的是:
TRACING
有一个数值(如#define TRACING 2 \n)
,cpp 有一个有效的表达式,并且我们没有改变值。TRACING
没有值(如#define TRACING \n
),预处理器将#if (+0)
计算为false
唯一不处理的情况是
TRACING
具有非数字值(ie、ON
)。 cpp 手册说“非宏的标识符...都被认为是数字零,”,其计算结果为false
。然而,在这种情况下,将其视为true
值更有意义。唯一正确的做法是布尔文字true
和false
。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 manualsays 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:
TRACING
has a numerical value (as in#define TRACING 2 \n)
, cpp has a valid expression, and we haven't changed the value.TRACING
has no value (as in#define TRACING \n
), the preprocessor evaluates#if (+0)
tofalse
The only case this doesn't handle is
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 tofalse
. In this case, however, it would make more sense to consider this atrue
value. The only ones that do the right thing are the boolean literalstrue
andfalse
.虽然迟到了,但我发现了一个很好的技巧来区分
:
如果
TRACING
为空,则表达式的计算结果为0--1
=> <代码>1。如果
TRACING
为 0,则表达式的计算结果为0-0-1
=>-1
我在
TRACING==-2
情况下添加了额外的检查,这将使第一个测试通过。当然,这不适用于字符串文字。
Late to the party but I found a nice trick to distinguish
from
like this:
If
TRACING
is empty, the expression evaluates to0--1
=>1
.If
TRACING
is 0, the expression evaluates to0-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.
会
成功吗?
Would
do the trick?