#define SAFE_RELEASE_NULL(p) do { if(p) { (p)->release(); (p) = 0; } } while(0)
上面这个宏定义和下面的有区别吗?
#define SAFE_RELEASE_NULL(p) if(p) { (p)->release(); (p) = 0; }
上面这个宏定义和下面的有区别吗?
#define SAFE_RELEASE_NULL(p) if(p) { (p)->release(); (p) = 0; }

1 loggerhead Oct 20, 2013 有. 比如: if (1) SAFE_RELEASE_NULL(arg); else doSomeThing(); 后者末尾加分号就错了 |
2 xpfd Oct 20, 2013 @loggerhead 正解,能不用宏的地方尽量不要用,尽量使用inline和enmu代替 |
3 iEverX Oct 20, 2013 @loggerhead 所说确实是一个方面 最重要应该是,while(0)把一个语句块包起来,整个宏就就类似于一个语句一样。 #define A(arg) dosth(arg);doanother(arg) #define B(arg) do { dosth(arg);doanother(arg); } while(0) if (xxx) A(arg); // 这一行和下一行,doanother(arg)的执行有区别 if (xxx) B(arg); |
4 yangxin0 Oct 20, 2013 |
5 jokerlee Oct 20, 2013 do while{...}用在宏里还有一个作用是{}之间是一个独立的作用域,里面定义的变量不会污染外部作用域 |
6 xgdyhaiyang Oct 22, 2013 GCC 官方文档:Swallowing the Semicolon http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon |