gpt4 book ai didi

c++ - C++11 标准中的哪一部分规定了原始数据类型大小之间的相对顺序?

转载 作者:太空狗 更新时间:2023-10-29 19:47:39 26 4
gpt4 key购买 nike

我正在尝试查找 C++ 标准是否指定了各种类型的大小之间的关系。例如,这个答案在 https://stackoverflow.com/a/589599/1175080似乎声称:

sizeof(short int) <= sizeof(int) <= sizeof(long int)

另一个答案在 https://stackoverflow.com/a/589684/1175080有类似的说法:

sizeof(int) <= sizeof(long)

我正在浏览 n3337.pdf (我相信 C+11 标准的一个很好的代理)但我找不到保证这些不平等的特定语言。

n1256 (C99),我可以在 5.2.4.2.1 尺寸部分找到特定语言整数类型,它清楚地说明了每种类型的最小值和最大值,间接建立了大小之间的相对顺序。

这些不等式是在 C++ 标准中类似定义的,还是直接从 C 继承的?标准中哪里有这方面的语言?

最佳答案

在 n3337 中,该部分是 3.9.1 , [basic.fundamental]/2 ,第二段(重点是我的):

There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plain ints have the natural size suggested by the architecture of the execution environment44; the other signed integer types are provided to meet special needs.


请注意 5.2.4.2/1在 C 标准中,只定义了每个整数类型的最小范围,它不强制执行顺序——我可以让 int 从 -32767 到 32767 和 short 从 -2147483647 到2147483647 并且仍然符合本部分。

但是6.2.5.8更明确:

For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

6.3.1.1告诉你:

The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

关于c++ - C++11 标准中的哪一部分规定了原始数据类型大小之间的相对顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50506061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com