[英]<cstdint> vs std::size_t types
From the peeks I've made into boost
and libstdc++
, the libraries usually make use of std::size_t
and std::ssize_t
whenever the upper/lower limit of an unsigned/signed index is not known in advance.从我对
boost
和libstdc++
的std::ssize_t
只要事先不知道无符号/有符号索引的上限/下限,这些库通常会使用std::size_t
和std::ssize_t
。 My question is: Why not rather use uintmax_t
from <cstdint>
instead of std::size_t
and intmax_t
instead of std::ssize_t
?我的问题是:为什么不情愿使用
uintmax_t
从<cstdint>
代替std::size_t
和intmax_t
代替std::ssize_t
?
The former are part of the C++ standard, the latter are not.前者是 C++ 标准的一部分,后者不是。 More precisely, the
cstdint
header was only recently introduced (in C++11).更准确地说,
cstdint
头文件最近才被引入(在 C++11 中)。 The reason for this is that stdint.h
itself is part of C99, which is newer than C++98.这样做的原因是
stdint.h
本身是 C99 的一部分,它比 C++98 更新。
Because the size_t types are intended to describe the sizes of things.因为 size_t 类型旨在描述事物的大小。 Using them for sizes is more descriptive than uint_t.
将它们用于大小比 uint_t 更具描述性。
Also, it might be possible for an architecture to be limited to smaller sizes of things so size_t might not always be the biggest integer type.此外,架构可能仅限于较小尺寸的事物,因此 size_t 可能并不总是最大的整数类型。 Although I think that would be a bit weird.
虽然我觉得那会有点奇怪。
The C++11 standard (section 18.2) says: C++11 标准(第 18.2 节)说:
(5).
(5). The type
ptrdiff_t
is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object....类型
ptrdiff_t
是一个实现定义的有符号整数类型,它可以在一个数组对象中保存两个下标的差异......(6).
(6). The type
size_t
is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.类型
size_t
是实现定义的无符号整数类型,它足够大以包含任何对象的字节大小。(7).
(7). [ Note: It is recommended that implementations choose types for
ptrdiff_t
andsize_t
whose integer conversion ranks (4.13) are no greater than that ofsigned long int
unless a larger size is necessary to contain all the possible values.[注意:建议实现为
ptrdiff_t
和size_t
选择整数转换等级 (4.13) 不大于有signed long int
除非需要更大的大小来包含所有可能的值。 —end note ]——尾注]
From this we see that:由此我们看到:
size_t
is specifically for byte-sizes of objects, and its companion ptrdiff_t
is specifically for math with array indices. size_t
专门用于对象的字节大小,它的配套ptrdiff_t
专门用于具有数组索引的数学。 uintmax_t
, on the other hand, is the largest unsigned integral type.另一方面,
uintmax_t
是最大的无符号整数类型。
Depending on the platform uintmax_t
could be larger than size_t
.根据平台
uintmax_t
可能大于size_t
。
We also know that:我们也知道:
sizeof
returns a size_t
, and the STL container size_type
s are typically identical to size_t
, so it makes sense to use size_t
in code that deals with sizeof
or STL containers. sizeof
返回一个size_t
,并且 STL 容器size_type
s 通常与size_t
相同,因此在处理sizeof
或 STL 容器的代码中使用size_t
是有意义的。
Now mix in the fact the <cstdint>
is new-ish to C++, and I think it's pretty clear why established libraries like Boost have been using size_t
.现在混合
<cstdint>
是 C++ 的新事实,我认为很清楚为什么像 Boost 这样的成熟库一直在使用size_t
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.