C 中整数提升和整数转换之间有什么区别?

发布于 2024-10-11 10:13:57 字数 353 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-10-18 10:13:57

认为这种区别很重要,因为两者不属于同一转换类别并且具有不同的排名(参见13.3.3.1.1,标准转换序列)。当涉及到重载解析时,排名会产生影响:

标准转化序列按其排名排序:完全匹配是比促销更好的转化,
这是比转换更好的转换。

最后,我相信正是 4.5 和 4.7 之间的区别使得以下代码变得明确:

#include <iostream>

void foo(int i)            { std::cout << "foo(int)" << std::endl; }
void foo(unsigned short i) { std::cout << "foo(unsigned short)" << std::endl; }

int main()
{
    foo(static_cast<short>(1));
}
  • shortint 是一个提升(因此具有提升等级)
  • Shortunsigned 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 :

Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion,
which is a better conversion than a Conversion.

In the end, I believe it is the distinction between 4.5 and 4.7 that makes the following code unambiguous :

#include <iostream>

void foo(int i)            { std::cout << "foo(int)" << std::endl; }
void foo(unsigned short i) { std::cout << "foo(unsigned short)" << std::endl; }

int main()
{
    foo(static_cast<short>(1));
}
  • short to int is a promotion (thus having promotion rank)
  • short to unsigned short is a conversion (thus having conversion rank)

In the end, this code calls foo(int) because it is a better candidate.

Oo萌小芽oO 2024-10-18 10:13:57

提升发生在算术和其他运算期间。当仅将一种整型类型存储在另一种整型类型中时,就会发生转换。

Promotions occur during arithmetic and other operations. Conversions occur when merely storing one integral type inside another.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文