#define update_weight(bits, weight, source, result) \
    if (source && result) { \
if ((source ^ result) >= 0) { if (weight++ == (1 << bits)) weight--; } \
else if (weight-- ==  (-(1 << bits))) weight++; \
    }此宏定义是否有意义,请讨论!

解决方案 »

  1.   

    inline void update_weight(bits, weight, source, result)
    {
        if (source && result)
        { 
    if ((source ^ result) >= 0)
             {
                   if (weight++ == (1 << bits)) weight--;
             } 
    else if (weight-- ==  (-(1 << bits)))
             {
                    weight++;
             }
        }
    }给几个参数加上数据类型即可。inline函数的效率很高,不会比宏差。
      

  2.   

    用宏定义容易出错最好用 inline
      

  3.   

    不要定义这么复杂的宏,用inline函数效果一样,实际上编译器对于inline函数也是就地展开,并且定义成inline函数能进行类型检查,比较安全
      

  4.   

    宏不要定义的太复杂,复杂的宏在使用时由于运算符优先级等因素容易出错,且没有类型检查机制,所以建议把它定义成inline函数。
      

  5.   

    我是希望大家看一下,weight的值时候会有变化呢