为什么在#MACROS中{} while(0)? [英] Why do{}while(0) in #MACROS?

查看:73
本文介绍了为什么在#MACROS中{} while(0)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在不同的地方写#MACROS的人使用null

do / while。例如:


做{POINTER = new CONSTRUCTOR; \

if(POINTER == 0){errno = ENOMEM; ACE_THROW_INT(EXCEPTION); } \

}而(0)


这取得了什么成果?


-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell

I''ve noticed in different places where people who write #MACROS use a null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \
} while (0)

What does that accomplish?

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
我注意到在不同的地方写#MACROS的人使用null
做/同时。例如:

做{POINTER = new CONSTRUCTOR; \\ /
if(POINTER == 0){errno = ENOMEM; ACE_THROW_INT(EXCEPTION);这是完成了什么?
I''ve noticed in different places where people who write #MACROS use a null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \
} while (0)

What does that accomplish?



它让你有一个宏定义一个本地范围,限制一个'if''

语句或两者 - 并且仍以分号结束。


HTH,

- ag


-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays


It enables you to have a macro define a local scope, confine an `if''
statement or both -- and still end it with a semicolon.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


Artie Gold写道:
Artie Gold wrote:
Steven T. Hatton写道:
Steven T. Hatton wrote:
我注意到在不同的地方写#MACROS的人使用
null 做/同时。例如:

做{POINTER = new CONSTRUCTOR; \\ /
if(POINTER == 0){errno = ENOMEM; ACE_THROW_INT(EXCEPTION);这是完成了什么?
I''ve noticed in different places where people who write #MACROS use a
null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \
} while (0)

What does that accomplish?


它让你有一个宏定义一个局部范围,限制一个`if' '
声明或两者兼而有之 - 仍然以分号结束。

HTH,
- ge


It enables you to have a macro define a local scope, confine an `if''
statement or both -- and still end it with a semicolon.

HTH,
--ag



为什么不简单使用''{''和''}''?

#include< iostream>


int main(){

{

if(true){

std :: cout<< 我不明白。 << std :: endl;

}

};

}


-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


Steven T. Hatton写道:
Steven T. Hatton wrote:
Artie Gold写道:
Artie Gold wrote:
Steven T. Hatton写道:
Steven T. Hatton wrote:
我注意到在不同的地方写#MACROS的人使用
一个/
do / while。例如:

做{POINTER = new CONSTRUCTOR; \\ /
if(POINTER == 0){errno = ENOMEM; ACE_THROW_INT
(例外); } \} while(0)

这取得了什么成果?
I''ve noticed in different places where people who write #MACROS use
a null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT
(EXCEPTION); } \ } while (0)

What does that accomplish?


它使你可以让宏定义一个局部范围,限制一个`if''
声明或两者 - 并且仍以分号结束。

HTH,
- ge


It enables you to have a macro define a local scope, confine an `if''
statement or both -- and still end it with a semicolon.

HTH,
--ag


为什么不简单地使用''{''和' '}''?
#include< iostream>

int main(){
{
if(true){
std :: cout << 我不明白。 << std :: endl;
}
};
}


Why not simpley use ''{'' and ''}''?
#include <iostream>

int main () {
{
if(true) {
std::cout << "I don''t get it." << std::endl;
}
};
}




试一试。比较结果。


#define LIMITSCOPE(what){int what = 42; printf(" LIMITING" ## #what); }

#include< stdio.h>

int main(int argc,char * argv []){

if(argc> ; 1)

LIMITSCOPE(abc); //注意这一行

else

printf(不再多);

}

---------------------------------

#define LIMITSCOPE(what)do {\\ \\ / $
int what = 42; printf(" LIMITING" ## #what); } while(0)

#include< stdio.h>

int main(int argc,char * argv []){

if(argc> 1)

LIMITSCOPE(abc); //注意这一行

else

printf(不再多);

}

V



Try it. Compare the results.

#define LIMITSCOPE(what) { int what = 42; printf("LIMITING " ## #what); }
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc > 1)
LIMITSCOPE(abc); // pay attention to this line
else
printf("not more");
}
---------------------------------
#define LIMITSCOPE(what) do { \
int what = 42; printf("LIMITING " ## #what); } while(0)
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc > 1)
LIMITSCOPE(abc); // pay attention to this line
else
printf("not more");
}
V


这篇关于为什么在#MACROS中{} while(0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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