条件代码 [英] Conditional code

查看:77
本文介绍了条件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个功能debug printf它的作用类似于printf,但是如果没有定义_DEBUG标志以及要打印的字符串

,那么它将被省略。


现在我定义了这个:


#ifdef _DEBUG

void dbgprintf(char * szFormat,...);

#else

#define dbgprintf

#endif


这样可行,但在某些编译器下,例如。 gcc它会给我一个巨大的b $ b b警告,说声明没有效果。这是真的,

但我不需要提醒,因为这是意图。有什么

你有什么好主意我怎么能改进宏来停止编译器

给这个?


谢谢提前。

- John

Lets say you have a function "debug printf" which works like printf but will
get left out if _DEBUG flag is not defined along with the strings which are
to be printed.

Now I defined this:

#ifdef _DEBUG
void dbgprintf(char *szFormat, ...);
#else
#define dbgprintf
#endif

This works fine but under some compilers eg. gcc it will give me a huge
bunch of warnings saying the statement has no effect. This is ofcourse true,
but I don''t need to be reminded since this was the intention. Does any of
you have good ideas how I can improve the macro to stop the compiler from
giving this?

Thanks in advance.
-- John

推荐答案



" John Smith" <乔******** @ x-formation.com>在消息中写道

news:42 ********************* @ dread11.news.tele.dk。 ..

"John Smith" <jo********@x-formation.com> wrote in message
news:42*********************@dread11.news.tele.dk. ..
让我们说你有一个函数debug printf如果没有定义_DEBUG标志以及要打印
的字符串,那么它就像printf一样工作但
将被遗漏。

现在我定义了这个:

#ifdef _DEBUG


请注意,以上名称是为ISO C ++中的实现保留的,

因为它以下划线和首都开头信件。我想这没关系

如果你正在使用编译器内置的调试标志,但是我会建议不要使用b $ b。我总是只定义DEBUG或MY_DEBUG当我想要

做类似这样的事情时,这样就可以了。

void dbgprintf(char * szFormat,...) ;
#else
#define dbgprintf
#endif

这个工作正常,但在一些编译器下,例如。 gcc它会给我一大堆警告说声明没有效果。这是
的真实情况,但我不需要提醒,因为这是意图。你是否有任何好主意如何改进宏以阻止编译器给出这个?
Lets say you have a function "debug printf" which works like printf but will get left out if _DEBUG flag is not defined along with the strings which are to be printed.

Now I defined this:

#ifdef _DEBUG
Note that the above name is reserved for the implementation in ISO C++,
since it begins with an underscore and a capital letter. I guess this is ok
if you are making use of the compilers built-in debug flags, but I would
recommend against it. I always just define DEBUG or MY_DEBUG when I want to
do something like this example, and that works just fine.
void dbgprintf(char *szFormat, ...);
#else
#define dbgprintf
#endif

This works fine but under some compilers eg. gcc it will give me a huge
bunch of warnings saying the statement has no effect. This is ofcourse true, but I don''t need to be reminded since this was the intention. Does any of
you have good ideas how I can improve the macro to stop the compiler from
giving this?




我不是明白为什么你会看到不止一条错误信息...你是否在你的标题上使用包含警戒的
(我假设上面显示的确是

一个头文件在某处你的计划。


无论如何,为什么不写:

// ...

#else

#define dbgprintf / ** /


或类似的东西?


HTH,

Dave Moore



I don''t understand why you would see more than one error message ... are you
using include guards on your headers (I assume the above appears in exactly
one header file somewhere in your program.

In any case, why not just write:
// ...
#else
#define dbgprintf /**/

or something like that?

HTH,

Dave Moore


" John Smith"< jo ******** @ x-formation.com>在消息中写道

news:42 ********************* @ dread11.news.tele.dk ... ..
"John Smith" <jo********@x-formation.com> wrote in message
news:42*********************@dread11.news.tele.dk. ..
让我们假设你有一个函数debug printf,它的作用类似于printf,但是如果没有定义_DEBUG标志以及要打印
的字符串,
将被遗漏。
现在我定义d这个:

#ifdef _DEBUG
void dbgprintf(char * szFormat,...);
#else
#define dbgprintf
#endif
Lets say you have a function "debug printf" which works like printf but will get left out if _DEBUG flag is not defined along with the strings which are to be printed.

Now I defined this:

#ifdef _DEBUG
void dbgprintf(char *szFormat, ...);
#else
#define dbgprintf
#endif




1.您可以在您的代码中使用


dbgprintf(" Print this string");


并定义


#ifdef _DEBUG

void dbgprintf(whatever_arguments ...)

{< br $> b $ b printf(...

}

#else // #ifdef _DEBUG

void dbgprintf(whatever_arguments .. 。)

{

//什么都不做。

}

#endif // #ifdef _DEBUG


2.就个人而言,我对这种方法感到担忧。考虑

以下

dbgprintf("%d",my_terribly_expensive_operation());



dbgprintf ("%d",my_operation_with_various_side_effects());


然后编译器即使在发布模式下也会调用my_terribly_expensive_operation()或

my_operation_with_various_side_effects() - 可能不是我想要的b $ b。

所以我通常将dbgprintf定义为宏itsel - 这样编译器就可以完全消除它的参数。另一个可能是个人品味的问题 -

区分调试和跟踪可能会更好:首先检查内部不变量;

第二个输出诊断信息。就个人而言,我认为这些应该是正交的。在某些情况下,我只能在发布模式下找到错误(例如,由于线程争用,

,调试模式可以完全改变)。至少
,我通常尝试使用正交调试和跟踪宏。



1. You could use in your code

dbgprintf("Print this string");

and define

#ifdef _DEBUG
void dbgprintf(whatever_arguments ...)
{
printf(...
}
#else // #ifdef _DEBUG
void dbgprintf(whatever_arguments ...)
{
// Do nothing.
}
#endif // #ifdef _DEBUG

2. Personally, I''m apprehensive about this approach at all. Consider the
following
dbgprintf("%d", my_terribly_expensive_operation());
or
dbgprintf("%d", my_operation_with_various_side_effects());

Then the compiler would call my_terribly_expensive_operation() or
my_operation_with_various_side_effects() even in release mode - probably not
what I want.
So I usually define dbgprintf as a macro itsel - this way the compiler can
eleminate its arguments completely.

3. Another matter which is possibly personal taste - it might be better to
distinguish between debug and trace: the first checks internal invariants;
the second outputs diagnostic information. Personally, I think these should
be orthogonal. There are cases where I find bugs in release mode only (e.g.,
because of thread races, which debug mode can change entirely). At the very
least, I usually try to have orthogonal debug and trace macros.


John Smith写道:
John Smith wrote:

这可以正常工作,但在一些编译器下,例如。 gcc它会给我一大堆警告说声明没有效果。

This works fine but under some compilers eg. gcc it will give me a huge
bunch of warnings saying the statement has no effect.




关闭警告。你比一些匿名编译器更了解

writer你的代码是否符合你的意思。


-


Pete Becker

Dinkumware,Ltd。( http:// www。 dinkumware.com


这篇关于条件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆