C++ size_t 和 std::size_t 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5813700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between size_t and std::size_t
提问by Mankarse
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?
之间有什么区别size_t
和std::size_t
,当他们要使用,任何其他差异化功能在他们的声明,其中条款?
采纳答案by Nawaz
C's size_t
and C++'s std::size_t
are both same.
Csize_t
和 C++std::size_t
都是一样的。
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 typeof the resultof the sizeofoperator.
在 C 中,它<stddef.h>
在 C++中和在 C++ 中定义,其定义<cstddef>
的内容与 C 头文件相同(请参阅下面的引用)。其定义为无符号整数类型的的结果的的的sizeof运算符。
C Standard says in §17.7/2,
C 标准在 §17.7/2 中说,
size_t which is the unsigned integer typeof the resultof the sizeofoperator
为size_t其是无符号整数类型的的结果的的的sizeof操作者
And C++ Standard says (about cstddef
header) in §18.1/3,
C++ 标准cstddef
在 §18.1/3 中说(关于标题),
The contents are the same as the Standard C library header , with the following changes.
内容与标准 C 库头文件相同,但有以下变化。
So yeah, both are same; the only difference is that C++ defines size_t
in std
namespace.
所以是的,两者都是一样的;唯一的区别是 C++size_t
在std
命名空间中定义。
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.
另请注意,上面的行还说“具有以下更改”,这不是指size_t
. 它更确切地说是指 C++ 对语言(C 中不存在)所做的新添加(主要是),这些添加也定义在同一标题中。
Wikipedia has very good info about range and storage size of size_t:
维基百科有关于 size_t 的范围和存储大小的非常好的信息:
Range and storage size of size_t
The actualtype of size_t is platform-dependent; a common mistakeis 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.
size_t 的范围和存储大小
size_t的实际类型是 平台相关的;一个常见的错误是假设 size_t 与 unsigned int 相同,这会导致编程错误,例如,当从 32 位架构迁移到 64 位架构时,[3][4]。
根据 1999 ISO C 标准 (C99),size_t 是至少 16 位的无符号整数类型。
And the rest you can read from this pageat wikipedia.
其余的你可以在维基百科的这个页面上阅读。
回答by Michael Burr
From C++03 "17.4.3.1.4 Types":
来自 C++03“17.4.3.1.4 类型”:
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.
对于标准 C 库(脚注 169)中的每个类型 T,类型 ::T 和 std::T 保留给实现,并且在定义时,::T 应与 std::T 相同。
And footnote 169:
和脚注 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.
这些类型是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。
回答by hifier
std::size_tis in fact stddef.h's size_t.
std::size_t实际上是stddef.h的size_t。
cstddefgives the following:
cstddef给出以下内容:
#include <stddef.h>
namespace std
{
using ::ptrdiff_t;
using ::size_t;
}
...effectively bringing the previous definition into the std namespace.
...有效地将先前的定义引入 std 命名空间。