is size_t always unsigned?

后端 未结 6 1861
陌清茗
陌清茗 2020-12-01 07:07

As title: is size_t always unsigned, i.e. for size_t x, is x always >= 0 ?

6条回答
  •  自闭症患者
    2020-12-01 07:51

    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).

    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.

    The 1989 ANSI C standard (ANSI C) doesn't mention a minimal size or recommended conversion rank.

    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:

    The contents are the same as the Standard C library header [...]

    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):

    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.

    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.

    Please also consider the following quote from section 1.2 of C++98:

    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.

提交回复
热议问题