空陈述 [英] Empty statement

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

问题描述

您好,

我希望有类似的东西


#ifdef VERSION_DEBUG

#define DEBUG(x)DebugFnc (x)

#end


#ifdef VERSION_RELEASE

#define DEBUG(x)< empty-statement>

#end


但是我不知道如何实现空语句..问题,

课程是在当宏被调用时,额外的分号,

DEBUG(x);在发布版本中。其中一个可能的解决方案是设置


#define DEBUG(x)do {} while(0)


但我不确定编译器是否足够智能

来优化它。还有其他任何想法吗?


-

Maksim Sipos

Hello,
I would like to have something like

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end

but I''m not sure how to implement the empty statement.. the problem, of
course is in the extra semicolon when the macro is called as in,
DEBUG(x); in release version. One of the possible solutions is to set

#define DEBUG(x) do{}while(0)

but I am not sure whether the compiler is smart enough
to optimize this. Any other ideas, comments?

--
Maksim Sipos

推荐答案

怎么样:


#define DEBUG(x);


这有用吗?


-

gabriel
What about:

#define DEBUG(x) ;

Does this work?

--
gabriel


Maksim Sipos写道:
Maksim Sipos wrote:

您好,
我想要一些像

#ifdef VERSION_DEBUG
#define DEBUG(x)DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x)< empty-statement>
#end

但我不知道如何实现空语句..当问题调用宏时,问题当然是在额外的分号中,
DEBUG(x);在发布版本。

Hello,
I would like to have something like

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end

but I''m not sure how to implement the empty statement.. the problem, of
course is in the extra semicolon when the macro is called as in,
DEBUG(x); in release version.



[...]


只需将其定义为空:


#define DEBUG(x)


为什么是额外的分号?发布版本中的一个问题?


你可以提供一些不起作用的代码示例吗?


-


+ --------- + ----------------------------- ----- + ----------------------------- +

|肯尼斯| kenbrody at spamcop.net | 表达的意见|

| J. | http://www.hvcomputer.com |这里不一定是|
|布罗迪| http://www.fptech.com |那些fP技术。 |

+ --------- + ------------------------------ ---- + ----------------------------- +


[...]

Simply define it as nothing:

#define DEBUG(x)

Why is "the extra semicolon" a problem in the release version?

Can you give a sample of code where it doesn''t work?

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+

Maksim Sipos写道:
Maksim Sipos wrote:
你好,
我想有类似的东西

#ifdef VERSION_DEBUG
#define DEBUG(x)DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x)< empty-statement>
#end


所需要的只是


#define DEBUG(x)/ *这里没什么* /

但是我''我不确定如何实现空语句..问题,当然,在调用宏时,在额外的分号中,
DEBUG(x);在发布版本中。


分号不会引起任何问题,因为它本身就是一个有效的空

语句。例如


int foo(无效)

{

int i; ; ; ;;;;

;

;;;;

for(i = 0; i< 10; ++ i);

if(i> 10){

;

}

返回i;

}


完全合法。现在,想象一下,每个分号上面没有

终止语句的是DEBUG(无论如何)。

其中一个可能的解决方案是设置

#define DEBUG(x)do {} while(0)

但我不确定编译器是否足够聪明以优化它。还有其他任何想法,评论吗?
Hello,
I would like to have something like

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end
All that is required is

#define DEBUG(x) /* nothing here */
but I''m not sure how to implement the empty statement.. the problem, of
course is in the extra semicolon when the macro is called as in,
DEBUG(x); in release version.
The semicolon will not cause any problems since it is itself a valid "empty"
statement. For example

int foo(void)
{
int i; ; ; ;;;;
;
;;;;
for(i=0; i<10; ++i);
if(i > 10){
;
}
return i;
}

is perfectly legal. Now, imagine that each semicolon above which does not
terminate a statement is proceeded by DEBUG(whatever).
One of the possible solutions is to set

#define DEBUG(x) do{}while(0)

but I am not sure whether the compiler is smart enough
to optimize this. Any other ideas, comments?




这个陈述不应该被优化掉,因为意图是执行

do-while *至少*一次。这实际上是实现

调试宏的一种非常好的方法,因为它可以防止悬空的其他问题以及

为调试特定的变量引入了一个范围。例如,


if(pred)

DEBUG(stuff);

else

return 0;


如果你定义了


#define DEBUG(x)if(dbgEnabled)fprintf(stderr," DBG: x =%d \ n",x)


你会遇到问题。


/ david


-

安德烈,一个简单的农民,只有一件事在他的脑海中悄悄地沿着东墙悄悄地上涨:''安德烈,蠕动...安德烈,蠕动...安德烈,蠕动。''

- 未知



This statement should not be optimized away since the intent is to execute the
do-while *at least* once. This is actually a very good way to implement
debugging macros since it protects against the dangling else problem as well as
introduces a scope for debugging-specific variables. For example,

if(pred)
DEBUG(stuff);
else
return 0;

If you''ve defined

#define DEBUG(x) if(dbgEnabled) fprintf(stderr, "DBG: x=%d\n", x)

you''ll run into problems.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: ''Andre, creep... Andre, creep... Andre, creep.''
-- unknown


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

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