在ASSERT宏中使用do ... while(0) [英] Use of do...while(0) in ASSERT macro

查看:73
本文介绍了在ASSERT宏中使用do ... while(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助快速查询...


我已经看到ASSERT宏被定义为:

#define ASSERT(f) \

做{\

if(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\

FatalExit (0); \\ /

} while(0)\

当我在调试模式下对此进行补充时,编译器警告条件

表达式是常量。 ,因为while(0)。为什么这样定义ASSERT

宏?循环只运行一次所以为什么不摆脱

do..while并使用:

#define ASSERT(f)\

if(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\

FatalExit(0); \

这也消除了编译器的警告。


有没有理由使用第一个定义?


Martin

Can anyone help with a quick query...

I''ve seen the ASSERT macro defined as:
#define ASSERT(f) \
do { \
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); \
} while (0) \
When I comple this in debug mode the compiler warns "conditional
expression is constant", because of the while (0). Why is the ASSERT
macro defined this way? The loop only runs once so why not get rid of
the do..while and use:
#define ASSERT(f) \
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); \
This also gets rid of the compiler warning.

Is there any reason to use the first definition?

Martin

推荐答案

mr *** **@totalise.co.uk (Martin)写道:

#任何人都可以帮忙快速查询......



#我看到ASSERT宏被定义为:





#define ASSERT(f)\

#do {\

#if(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\

# FatalExit(0); \\ /

#}而(0)\





#当我在调试模式编译器警告条件

#表达式是常量,因为while(0)。为什么ASSERT

#宏以这种方式定义?循环只运行一次,所以为什么不摆脱

#the do..while并使用:





#define ASSERT(f)\

#if(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\

# FatalExit(0); \


为什么不尝试类似

如果(p)

ASSERT(q)

另外

s;

看看会发生什么。


你可以通过支撑来防止这种情况

#define ASSERT(f){\

if(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\

FatalExit(0); }

但是现在你必须记住,尽管ASSERT(q)看起来像是一个声明,

如果你把'';''放在它之后

if(p)

ASSERT(q);

else

s;

意外中断。


现在尝试上面的ASSERT的原始定义。


#这也消除了编译器警告。


右边是-w选项。


-

Derk Gwen http://derkgwen.250free.com/html/index.html

上帝'是一个狂热的狂热分子。
mr*****@totalise.co.uk (Martin) wrote:
# Can anyone help with a quick query...
#
# I''ve seen the ASSERT macro defined as:
#
#
# #define ASSERT(f) \
# do { \
# if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
# FatalExit (0); \
# } while (0) \
#
#
# When I comple this in debug mode the compiler warns "conditional
# expression is constant", because of the while (0). Why is the ASSERT
# macro defined this way? The loop only runs once so why not get rid of
# the do..while and use:
#
#
# #define ASSERT(f) \
# if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
# FatalExit (0); \

Why not try something like
if (p)
ASSERT(q)
else
s;
and see what happens.

You can prevent this by bracing
#define ASSERT(f) {\
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); }
but now you have to remember that although ASSERT(q) looks like a statement,
if you put a '';'' after it
if (p)
ASSERT(q);
else
s;
it breaks unexpectedly.

Now try the original definition of ASSERT in the above.

# This also gets rid of the compiler warning.

So does the right -w option.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
God''s a skeeball fanatic.




" Martin" <先生***** @ totalise.co.uk> écritdansle message de news:
cb ************************** @ posting.google.com ...
任何人都可以帮助快速查询...

我已经看到ASSERT宏被定义为:

#define ASSERT(f)\
do {\
if(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\
FatalExit(0); \\ n
} while(0)\

当我在调试模式下对此进行补充时,编译器警告条件
表达式是常量,因为while(0) 。为什么这样定义ASSERT
宏?循环只运行一次,所以为什么不摆脱并使用:

#define ASSERT(f)\
if(!(f)& ;& assertFailedOnLine(THIS_FILE,__LINE__))\
FatalExit(0); \

这也消除了编译器的警告。

有没有理由使用第一个定义?
Can anyone help with a quick query...

I''ve seen the ASSERT macro defined as:
#define ASSERT(f) \
do { \
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); \
} while (0) \
When I comple this in debug mode the compiler warns "conditional
expression is constant", because of the while (0). Why is the ASSERT
macro defined this way? The loop only runs once so why not get rid of
the do..while and use:
#define ASSERT(f) \
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); \
This also gets rid of the compiler warning.

Is there any reason to use the first definition?




我不知道怎么用英语打电话,所以我试着解释一下。

因为effets de bord因为在使用这个宏之后我们能找到的只是

是问题。

在某些情况下,宏的一部分包含在我们不喜欢的东西中不想要

到。


例如:

#define ADD(A,B)A + B

ADD(1,2)* 3预处理得到:1 + 2 * 3

或者我确定你想要:(1 + 2)* 3


所以,我们更喜欢使用:

#define ADD(A,B)((A)+(B))


当你使用函数时,你可以使用do {blablabla} while(0),即

同样的东西,添加到函数中。

I希望你能理解我的坏英语。



I don''t know how it''s call in english, so I try to explain it.
It because "effets de bord" tha are problem due to what we can find just
before an after the use of this macro.
In some cases, one part of the macro is included in something we don''t want
to.

example:
# define ADD(A, B) A + B

ADD(1, 2) * 3 get after preprocessing: 1 + 2 * 3
or I''m sure you want: (1 + 2) * 3

so, we prefer to use:
#define ADD(A, B) ((A) + (B))

When you use function, you can use the do { blablabla } while (0) that is
the same thing, addapted to function.
I hope you''ll understand however my bad english.


2003年11月15日03:34:20 -0800
mr ***** @ totalise.co.uk (马丁)写道:
On 15 Nov 2003 03:34:20 -0800
mr*****@totalise.co.uk (Martin) wrote:
任何人都可以帮助快速查询...

我已经看到ASSERT宏被定义为:

#define ASSERT(f)\\ \\ {/
如果(!(f)&& assertFailedOnLine(THIS_FILE,__LINE__))\
FatalExit(0); \\ n
} while(0)\

当我在调试模式下对此进行补充时,编译器警告条件
表达式是常量,因为while(0) 。为什么这样定义ASSERT
宏?循环只运行一次,所以为什么不摆脱并使用:

#define ASSERT(f)\
if(!(f)& ;& assertFailedOnLine(THIS_FILE,__LINE__))\
FatalExit(0); \

这也消除了编译器的警告。

有没有理由使用第一个定义?
Can anyone help with a quick query...

I''ve seen the ASSERT macro defined as:
#define ASSERT(f) \
do { \
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); \
} while (0) \
When I comple this in debug mode the compiler warns "conditional
expression is constant", because of the while (0). Why is the ASSERT
macro defined this way? The loop only runs once so why not get rid of
the do..while and use:
#define ASSERT(f) \
if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
FatalExit (0); \
This also gets rid of the compiler warning.

Is there any reason to use the first definition?




if(x)

for(y = x; y> 0& a amp; a [x]!= TARGET]; y--)

ASSERT (a [x] ==坏);

其他

y = -1;


鉴于你的定义(减去虚假的\\ \\)如果(x =

为(y = x; y> 0&& a [x]!= TARGET],则扩展为

; y-- )

if(!(a [x] == BAD)&& assertFailedOnLine(THIS_FILE,__LINE__))

FatalExit(0);;

else

y = -1;


注意FatalExis(0)之后的两个分号会使else无效。

摆脱你得到的虚假分号

if(x)

for(y = x; y> 0&& a [x]!= TARGET ]; y - )

if(!(a [x] == BAD)&& assertFailedOnLine(THIS_FILE,__LINE__))

FatalExit(0);

其他

y = -1;


这使得其他人与如果来自断言宏。鉴于

你得到的原始定义


if(x)

for(y = x; y> 0&& ; a [x]!= TARGET]; y--)

如果(!(a [x] == BAD)&& assertFailedOnLine(THIS_FILE,__LINE__))

FatalExit(0);

} while(0);

else

y = -1;


将按预期运行。


这就是为什么你会看到人们使用do {...}而(0)在宏中

当他们想要的东西就像一个返回类型的函数

of void。还要注意宏的结尾没有分号

定义。

-

Mark Gordon

付钱成为极客和高级软件开发人员

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
ASSERT(a[x]==BAD);
else
y=-1;

Given your definition (minus spurious \) this expands to
if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
if (!(a[x]==BAD) && assertFailedOnLine (THIS_FILE, __LINE__))
FatalExit (0);;
else
y=-1;

Note the two semicolons after FatalExis(0) which makes the else invalid.
Get rid of the spurious semicolon you get
if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
if (!(a[x]==BAD) && assertFailedOnLine (THIS_FILE, __LINE__))
FatalExit (0);
else
y=-1;

which makes the else associate with the if from the assert macro. Given
the original definition you get

if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
do { \
if (!(a[x]==BAD) && assertFailedOnLine (THIS_FILE, __LINE__))
FatalExit (0);
} while (0);
else
y=-1;

which will behave as expected.

This is why you will see people using do { ... } while (0) in macros
when they want something that behaves like a function with a return type
of void. Also note the lack of a semicolon at the end of the macro
definition.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.


这篇关于在ASSERT宏中使用do ... while(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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