size_t 和 std::size_t 之间的区别
size_t
和 std::size_t
在声明位置、何时使用以及任何其他区别功能方面有何区别?
What are the differences between size_t
and std::size_t
in terms of where they are declared, when they should be used and any other differentiating features?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C 的
size_t
和 C++ 的std::size_t
都是相同的。在 C 中,它在
中定义,在 C++ 中,它在
中定义,其内容与 C 头文件相同(参见下面的引用) )。它定义为 sizeof 运算符的结果的无符号整数类型。C 标准在 §17.7/2 中说,
C++ 标准说(关于
cstddef
标头)在第 18.1/3 节中,所以是的,两者是相同的; 唯一的区别是 C++ 在
std
命名空间中定义size_t
。另请注意,上面的行还显示“”,但有以下更改" 这不是指
size_t
。它更确切地说是指 C++ 在语言中添加的(大部分)新内容(C 中不存在),这些内容也在同一头文件中定义。维基百科有关于
size_t
的范围和存储大小的非常好的信息:其余内容您可以从维基百科的此页面阅读。
C's
size_t
and C++'sstd::size_t
are both same.In C, it's defined in
<stddef.h>
and in C++, its defined in<cstddef>
whose contents are the same as C header (see the quotation below). Its defined as unsigned integer type of the result of the sizeof operator.C Standard says in §17.7/2,
And C++ Standard says (about
cstddef
header) in §18.1/3,So yeah, both are same; the only difference is that C++ defines
size_t
instd
namespace.Please also notice that the above line also says "with the following changes" which isn't referring to
size_t
. Its rather referring to the new additions (mostly) made by C++ into the language (not present in C) which are also defined in the same header.Wikipedia has very good info about range and storage size of
size_t
:And the rest you can read from this page at Wikipedia.
来自 C++03“17.4.3.1.4 类型”:
脚注 169:
From C++03 "17.4.3.1.4 Types":
And footnote 169:
std::size_t 实际上是 stddef.h 的 size_t。
cstddef 给出以下内容:
...有效地将先前的定义引入 std 命名空间。
std::size_t is in fact stddef.h's size_t.
cstddef gives the following:
...effectively bringing the previous definition into the std namespace.