define abs(x) ((x<0)?-(x):(x)) 为何这样写


#define abs1(x) (x<0)?-x:x
#define abs2(x) ((x<0)?-x:x)
#define abs3(x) ((x<0)?-(x):x)
#define abs4(x) ((x<0)?-(x):(x))
#define abs5(x) (((x)<0)?-(x):(x))

区别

There are various related problems that the extra parentheses solve. I’ll go through them one by one:

Try: int y = abs( a ) + 2
Let’s assume you use:

define abs(x) (x<0)?-x:x


int y = abs( a ) + 2
This expands to int y = (a<0)?-a:a+2. The +2 binds only to the false result. 2 is only added when a is positive, not when it is negative. So we need parenthesis around the whole thing:

define abs(x) ( (x<0) ? -x : x )

Try: int y = abs(a+b);
But then we might have int y = abs(a+b) which gets expanded to int y = ( (a+b<0) ? -a+b : a+b). If a + b is negative then b is not negated when they add for the result. So we need to put the x of -x in parentheses.

define abs(x) ( (x<0) ? -(x) : x )

Try: int y = abs(a=b);
This ought to be legal (though bad), but it expands to int y = ( (a=b<0)?-(a=b):a=b ); which tries to assign the final b to the ternary. This should not compile. (Note that it does in C++. I had to compile it with gcc instead of g++ to see it fail to compile with the “invalid lvalue in assignment” error.)

define abs(x) ( (x<0) ? -(x) : (x) )

Try: int y = abs((a

define abs(x) ( ( (x) < 0) ? -(x) : (x) )

In the end, each instance of x is prone to some grouping problem that parentheses are needed to solve.

Common problem: operator precedence
The common thread in all of these is operator precedence: if you put an operator in your abs(…) invocation that has lower precedence then something around where x is used in the macro, then it will bind incorrectly. For instance, abs(a=b) will expand to a=b<0 which is the same as a=(b<0)… that isn’t what the caller meant.

The “Right Way” to Implement abs
Of course, this is the wrong way to implement abs anyways… if you don’t want to use the built in functions (and you should, because they will be optimized for whatever hardware you port to), then it should be an inline template (if using C++) for the same reasons mentioned when Meyers, Sutter, et al discuss re-implementing the min and max functions. (Other answers have also mentioned it: what happens with abs(x++)?)

Off the top of my head, a reasonable implementation might be:

template inline const T abs(T const & x)
{
return ( x<0 ) ? -x : x;
}
Here it is okay to leave off the parentheses since we know that x is a single value, not some arbitrary expansion from a macro.

Better yet, as Chris Lutz pointed out in the comments below, you can use template specialization to call the optimized versions (abs, fabs, labs) and get all the benefits of type safety, support for non-builtin types, and performance.

转载 : http://stackoverflow.com/questions/2025372/c-macro-question-x-vs-x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值