gpt4 book ai didi

c++ - size_t 和 std::size_t 之间的区别

转载 作者:IT老高 更新时间:2023-10-28 11:50:52 32 4
gpt4 key购买 nike

size_tstd::size_t 在声明的位置、应在何时使用以及任何其他区别特性方面有何区别?

最佳答案

C 的 size_t和 C++ 的 std::size_t都是一样的。

在 C 中,它在 <stddef.h> 中定义在 C++ 中,它在 <cstddef> 中定义其内容与 C header 相同(请参阅下面的引用)。其定义为 sizeof 运算符的result无符号整数类型

C 标准在 §17.7/2 中说,

size_t which is the unsigned integer type of the result of the sizeof operator

C++ 标准在 §18.1/3 中说(关于 cstddef header ),

The contents are the same as the Standard C library header , with the following changes.

是的,两者都是一样的; 唯一的区别是 C++ 定义了 size_tstd命名空间。

还请注意,上面的行也说“有以下更改”,这不是指size_t .它更确切地说是指 C++ 对语言(C 中不存在)的新添加(大部分),它们也在同一个标​​题中定义。


维基百科有关于 size_t 的范围和存储大小的非常好的信息:

Range and storage size of size_t

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors,[3][4] when moving from 32 to 64-bit architecture, for example.

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bits.

其余的可以从 this page 阅读。在维基百科。

关于c++ - size_t 和 std::size_t 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5813700/

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