#define DELETE_POINTER(p) do { if(NULL != p) delete p; p = NULL; }while(0)
int* p = new int(5); DELETE_POINTER(p);
#define PRINT_STRING(isDoc) if(isDoc) printDoc();
bool isReady = false; bool isDoc = true; if(isReady) PRINT_STRING(isDoc); else doOtherThing();
bool isReady = false; bool isDoc = true; if(isReady) if(isDoc) printDoc(); else doOtherThing();
原文:http://www.spongeliu.com/415.html
do{ ... }while(0) |
#define DOSOMETHING()\ foo1();\ foo2(); |
if(a]]>0) DOSOMETHING(); |
if(a]]>0) foo1(); foo2(); |
if(a]]>0) { foo1(); foo2(); }; |
#define DOSOMETHING() \ do{ \ foo1();\ foo2();\ }while(0)\ ... if(a]]>0) DOSOMETHING(); ... |
#define DOSOMETHING() ({\ foo1(); \ foo2(); \ }) |
int foo() { somestruct* ptr = malloc(...); dosomething...; if(error) { goto END; } dosomething...; if(error) { goto END; } dosomething...; END: free(ptr); return 0; } |
int foo() { somestruct* ptr = malloc(...); do{ dosomething...; if(error) { break; } dosomething...; if(error) { break; } dosomething...; }while(0); free(ptr); return 0; } |
#define EMPTYMICRO do{}while(0) |
do...while(0)在宏定义中的巧妙用法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/luoweifu/article/details/38563161