做/同时。例如: 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 lineelseprintf("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 lineelseprintf("not more");}V 这篇关于为什么在#MACROS中{} while(0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 16:10