为什么在SUCCEEDED宏中有这么多括号? [英] Why so many parentheses in SUCCEEDED macro?

查看:152
本文介绍了为什么在SUCCEEDED宏中有这么多括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows SDK的SUCCEEDED巨集:

  #define SUCCEEDED(hr)((HRESULT)(hr))> = 0)
----------------------- ^ ------------- ^ -----

清楚地说明,与其他宏有括号,以确保编译器意图的正确解释。 p>

我没有得到的是为什么(HRESULT)(hr) code> ^ 字符)。 hr 是括号括起来的,所以一些复杂的结构可以在那里, HRESULT 括号形成一个C风格的铸造,整个> = 构造也是括号,为什么(HRESULT)(hr)

解决方案

C标准使得转换的优先级高于比较,因此编译器不需要括号。



但是,人们读取宏定义,并将它们放在一起使得优先级显式,所以人们很容易看到,它是比较的结果HRESULT)hr),而不是转换比较的结果,而不必考虑优先级。


Windows SDK features SUCCEEDED macro:

#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
-----------------------^-------------^-----

clearly as with other macros there're parentheses to ensure right interpretation of the intent by compiler.

What I don't get is why there are parentheses around (HRESULT)(hr) (I marked them with ^ character). hr is parenthesized so that some complex construct can be there, HRESULT is parenthesized to form a C-style cast, then the whole >= construct is parenthesized as well, so why the extra pair of parentheses around (HRESULT)(hr)?

解决方案

The C standard puts the cast at a higher precedence than the comparison, so the parens are not required for the complier.

However, people read the macro definition, and putting them in makes the precedence explicit, so that it is obvious to people reading it that it is the result of comparing ((HRESULT)hr) with zero rather than casting the result of the comparison without having to think about the precedence.

这篇关于为什么在SUCCEEDED宏中有这么多括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆