size_t 和 std::size_t 之间的区别

发布于 2024-11-03 15:01:14 字数 83 浏览 2 评论 0原文

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

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

发布评论

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

评论(3

绝不服输 2024-11-10 15:01:14

C 的 size_t 和 C++ 的 std::size_t 都是相同的。

在 C 中,它在 中定义,在 C++ 中,它在 中定义,其内容与 C 头文件相同(参见下面的引用) )。它定义为 sizeof 运算符的结果无符号整数类型

C 标准在 §17.7/2 中说,

size_t 是 sizeof 运算符结果无符号整数类型

C++ 标准说(关于 cstddef 标头)在第 18.1/3 节中,

内容与标准 C 库头文件相同,有以下更改

所以是的,两者是相同的; 唯一的区别是 C++ 在 std 命名空间中定义 size_t

另请注意,上面的行还显示“”,但有以下更改" 这不是指 size_t。它更确切地说是指 C++ 在语言中添加的(大部分)新内容(C 中不存在),这些内容也在同一头文件中定义。


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

size_t的范围和存储大小

size_t实际类型是
依赖于平台常见错误
假设 size_t
unsigned int,这可能会导致
移动时的编程错误,[3][4]
从 32 位架构到 64 位架构,
示例。

根据 1999 ISO C
标准 (C99),size_t 是无符号的
至少 16 位的整数类型。

其余内容您可以从维基百科的此页面阅读。

C's size_t and C++'s std::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,

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

And C++ Standard says (about cstddef header) in §18.1/3,

The contents are the same as the Standard C library header <stddef.h>, with the following changes.

So yeah, both are same; the only difference is that C++ defines size_t in std 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:

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.

And the rest you can read from this page at Wikipedia.

弄潮 2024-11-10 15:01:14

来自 C++03“17.4.3.1.4 类型”:

对于标准 C 库(脚注 169)中的每个类型 T,类型 ::T 和 std::T 保留给实现,并且在定义时,::T 应与 std::T 相同。< /p>

脚注 169:

这些类型是clock_t、div_t、FILE、fpos_t、lconv、ldiv_t、mbstate_t、ptrdiff_t、sig_atomic_t、size_t、time_t、tm、va_list、wctrans_t、wctype_t 和 wint_t。

From C++03 "17.4.3.1.4 Types":

For each type T from the Standard C library (footnote 169), the types ::T and std::T are reserved to the implementation and, when defined, ::T shall be identical to std::T.

And footnote 169:

These types are clock_t, div_t, FILE, fpos_t, lconv, ldiv_t, mbstate_t, ptrdiff_t, sig_atomic_t, size_t, time_t, tm, va_list, wctrans_t, wctype_t, and wint_t.

云淡风轻 2024-11-10 15:01:14

std::size_t 实际上是 stddef.hsize_t

cstddef 给出以下内容:

#include <stddef.h>
namespace std 
{
  using ::ptrdiff_t;
  using ::size_t;
}

...有效地将先前的定义引入 std 命名空间。

std::size_t is in fact stddef.h's size_t.

cstddef gives the following:

#include <stddef.h>
namespace std 
{
  using ::ptrdiff_t;
  using ::size_t;
}

...effectively bringing the previous definition into the std namespace.

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