C 中整数提升和整数转换之间有什么区别?
C++ 标准的 4.5 节(整数提升)讨论了将整数类型转换为更高级别类型的具体情况。
C++ 标准的第 4.7 节(积分转换)开头为(第 4.7.1 条):
整数类型的右值可以转换为另一种整数类型的右值。枚举类型的右值可以转换为整数类型的右值。
据我了解,4.5 中描述的转换(可能除了项目符号 4.5.3(枚举))可以单独使用 4.7 节中的技术来执行:4.7.1 完全涵盖了 4.5.1 和 4.5.2; 4.5.4 由 4.7.4 涵盖。那么整个 4.5 部分的目的是什么?它能实现哪些额外转换?也许我缺少一些限制?
PS 我正在阅读 C++03 版本的标准。
Section 4.5 of the C++ standard (integer promotion) talks about specific cases of converting integral types to types with a higher rank.
Section 4.7 of the C++ standard (integral conversions) begins with (bullet 4.7.1):
An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type.
As far as I understand conversions described in 4.5 (maybe except for the bullet 4.5.3 (enums)) can be performed by using the techniques from 4.7 section alone: 4.5.1 and 4.5.2 are completely covered by 4.7.1; 4.5.4 is covered by 4.7.4. So what's the purpose of the entire 4.5 section? What additional conversions does it enable? Maybe I'm missing some restrictions?
P.S. I'm reading C++03 version of the standard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这种区别很重要,因为两者不属于同一转换类别并且具有不同的排名(参见13.3.3.1.1,标准转换序列)。当涉及到重载解析时,排名会产生影响:
最后,我相信正是 4.5 和 4.7 之间的区别使得以下代码变得明确:
short
到int
是一个提升(因此具有提升等级)Short
到unsigned Short
是一个转换(因此具有转换等级)。最后,此代码调用
foo(int)
因为它是更好的候选者。I think that the distinction is important because both do not fall in the same conversion category and have different rank (see 13.3.3.1.1, Standard conversion sequences). The rank makes a difference when it comes to overload resolution :
In the end, I believe it is the distinction between 4.5 and 4.7 that makes the following code unambiguous :
short
toint
is a promotion (thus having promotion rank)short
tounsigned short
is a conversion (thus having conversion rank)In the end, this code calls
foo(int)
because it is a better candidate.提升发生在算术和其他运算期间。当仅将一种整型类型存储在另一种整型类型中时,就会发生转换。
Promotions occur during arithmetic and other operations. Conversions occur when merely storing one integral type inside another.