现在有代码如下.

宏定义设置好:

#define ERROR_RET(x) error_code=x;return false;

然后期望在以后某处调用如下:

void fun()
{
    if(sth) ERROR_RET(eno);
    ....
}

编译护结果始终不理想.逻辑和想的不一样啊...查了别的代码好久,一直以为其它地方数据处理有问题.

终于轮到这个地方,然后哭着fix了下。

fix 很容易,如下:

-#define ERROR_RET(x) error_code=x;return false;
+#define ERROR_RET(x) {error_code=x;return false;}

宏和 inline function 是不一样的,它在编译的时候简单的替换相关代码。所以在原文中, fun 结果应该是这样的:

void fun()
{
    if(sth)
        error_code=x;
    return false;
    ....
}

所以所有调用 ERROR_RET() 的地方都是直接返回错。

其实要是我仔细看下 warning,本可以立即发现这个警告的。

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *