C++ size_t 总是无符号的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1089176/
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
is size_t always unsigned?
提问by peterchen
As title: is size_t always unsigned, i.e. for size_t x
, is x
always >= 0
?
正如标题: size_t 总是无符号的,即 for size_t x
,x
总是>= 0
?
采纳答案by Mehrdad Afshari
Yes. It's usuallydefined as something like the following (on 32-bit systems):
是的。它通常定义为如下内容(在 32 位系统上):
typedef unsigned int size_t;
Reference:
参考:
C++ Standard Section 18.1 defines size_t
is in <cstddef>
which is described in C Standard as <stddef.h>
.
C Standard Section 4.1.5 defines size_t
as an unsigned integral type of the result of the sizeof
operator
C ++标准第18.1节定义size_t
是在<cstddef>
其在C标准所描述的<stddef.h>
。
C 标准部分 4.1.5 将运算符size_t
的结果定义为无符号整数类型sizeof
回答by Christoph
According to the 1999 ISO C standard (C99), size_t
is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3).
根据 1999 ISO C 标准 (C99),size_t
是至少 16 位的无符号整数类型(参见第 7.17 和 7.18.3 节)。
The standard also recommends that size_t
shouldn't have an integer conversion rank greater than long
if possible, ie casting size_t
to unsigned long
is unproblematic if the recommendation is followed.
该标准还建议,如果可能的话,size_t
整数转换等级不应大于long
,即如果遵循建议,则转换size_t
为unsigned long
没有问题。
The 1989 ANSI C standard (ANSI C) doesn't mention a minimal size or recommended conversion rank.
1989 ANSI C 标准 (ANSI C) 没有提到最小尺寸或推荐的转换等级。
The 1998 ISO C++ standard (C++98) (as well as the current draft for C++0x) refers to the C standard. Section 18.1 reads:
1998 ISO C++ 标准 (C++98)(以及 C++0x 的当前草案)指的是 C 标准。第 18.1 节写道:
The contents are the same as the Standard C library header
<stddef.h>
[...]
内容与标准 C 库头文件相同
<stddef.h>
[...]
According to section 1.2, this means the library as defined by the 1990 ISO C standard (C90), including its first amendment from 1995 (C95):
根据第 1.2 节,这意味着由 1990 ISO C 标准 (C90) 定义的库,包括其 1995 年的第一次修订 (C95):
The library described in clause 7 of ISO/IEC 9899:1990 and clause 7 of ISO/IEC 9899/Amd.1:1995 is hereinafter called the Standard C Library.
ISO/IEC 9899:1990 第 7 条和 ISO/IEC 9899/Amd.1:1995 第 7 条中描述的库在下文中称为标准 C 库。
The parts regarding size_t
should be inherited from ANSI C: Frontmatter and section numbering aside, the standards for C90 and ANSI C are identical. I'd need a copy of the normative amendment to be sure that there weren't any relevant changes to stddef.h
, but I doubt it. The minimal size seems to be introduced with stdint.h
, ie C99.
有关部分size_t
应继承自 ANSI C:Frontmatter 和节编号,C90 和 ANSI C 的标准是相同的。我需要一份规范性修正案的副本,以确保没有任何相关更改stddef.h
,但我对此表示怀疑。最小尺寸似乎是通过stdint.h
,即 C99引入的。
Please also consider the following quote from section 1.2 of C++98:
还请考虑 C++98 第 1.2 节中的以下引用:
All standards are subject to revision, and parties to agreements based on this International Standard are encouraged to investigate the possibility of applying the most recent editions of the standards indicated below.
所有标准都会被修订,鼓励基于本国际标准达成协议的各方调查应用下列标准最新版本的可能性。
回答by nos
Yes, size_t is guaranteed to be an unsigned type.
是的, size_t 保证是无符号类型。
回答by Michael Burr
According to the standard it is unsigned, however I recall that some older implementations used a signed type for the typedef.
根据标准,它是无符号的,但是我记得一些较旧的实现为 typedef 使用了有符号类型。
From an older GCC doc:
来自较旧的 GCC 文档:
There is a potential problem with the size_t type and versions of GCC prior to release 2.4. ANSI C requires that
size_t
always be an unsigned type. For compatibility with existing systems' header files, GCC definessize_t
instddef.h
to be whatever type the system'ssys/types.h
defines it to be. Most Unix systems that definesize_t
insys/types.h
, define it to be a signed type. Some code in the library depends onsize_t
being an unsigned type, and will not work correctly if it is signed
2.4 版之前的 GCC 的 size_t 类型和版本存在潜在问题。ANSI C 要求
size_t
始终是无符号类型。为了与现有系统的头文件兼容,GCC 将size_t
in定义stddef.h
为系统sys/types.h
定义的任何类型。大多数定义size_t
in 的Unix 系统将其定义sys/types.h
为有符号类型。库中的某些代码取决于size_t
是否为无符号类型,如果有符号将无法正常工作
I'm not sure how important it would be to guard against that. My code assumes it's unsigned.
我不确定防范这种情况有多重要。我的代码假定它是未签名的。
回答by njsf
The size_t should follow the same definition as the C standard, and in several places in the C++ standard it implies it's unsigned natura (particularly in the allocator template argument definitions).
size_t 应该遵循与 C 标准相同的定义,并且在 C++ 标准的几个地方,它暗示它是无符号的自然(特别是在分配器模板参数定义中)。
On the C++ Standard, section 18.1 (ISO/IEC 14882 - First edition 1998-01-01):
关于 C++ 标准,第 18.1 节(ISO/IEC 14882 - 第一版 1998-01-01):
Table 15 lists as defined types: ptrdiff_t and size_t
表 15 列出了定义的类型:ptrdiff_t 和 size_t
3 The contents are the same as the Standard C library header , with the following changes: 4 The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10).
3 内容与标准 C 库头文件相同,但有以下变化: 4 宏 NULL 是本国际标准 (4.10) 中实现定义的 C++ 空指针常量。
The macro offsetof accepts a restricted set of type arguments in this International Standard. type shall be a POD structure or a POD union (clause 9). The result of applying the offsetof macro to a field that is a static data member or a function member is undefined. SEE ALSO: subclause 5.3.3, Sizeof, subclause 5.7, Additive operators, subclause 12.5, Free store, and ISO C subclause 7.1.6.
宏 offsetof 在本国际标准中接受一组受限制的类型参数。类型应为 POD 结构或 POD 联合(第 9 条)。将 offsetof 宏应用于作为静态数据成员或函数成员的字段的结果是未定义的。还请参见:第 5.3.3 条,Sizeof,第 5.7 条,加法运算符,第 12.5 条,自由存储和 ISO C 子条 7.1.6。
回答by Menno Deij - van Rijswijk
Oh, this is just terrible:
哦,这太可怕了:
vector<MyObject> arr;
Fill(arr);
size_t size = arr.size();
for(size_t i = 1; i < size - 1; ++i)
{
auto obj = arr[i];
auto next = arr[i+1];
}
Now contemplate the use case where arr is empty.
现在考虑 arr 为空的用例。