Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::size_t

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
       
      Defined in header<cstddef>
      Defined in header<cstdio>
      Defined in header<cstdlib>
      Defined in header<cstring>
      Defined in header<ctime>
      Defined in header<cuchar>
      (since C++17)
      Defined in header<cwchar>
      typedef/* implementation-defined */ size_t;

      std::size_t is the unsigned integer type of the result of the following operators:

      (since C++11)

      If a program attempts to form an oversized type (i.e., the number of bytes in itsobject representation exceeds the maximum value representable instd::size_t), the program is ill-formed.

      The bit width ofstd::size_t is not less than 16.

      (since C++11)

      Contents

      [edit]Notes

      std::size_t can store the maximum size of a theoretically possible object of any type (including array). On many platforms (an exception is systems with segmented addressing)std::size_t can safely store the value of any non-member pointer, in which case it is synonymous withstd::uintptr_t.

      std::size_t is commonly used for array indexing and loop counting. Programs that use other types, such asunsignedint, for array indexing may fail on, e.g. 64-bit systems when the index exceedsUINT_MAX or if it relies on 32-bit modular arithmetic.

      When indexing C++ containers, such asstd::string,std::vector, etc, the appropriate type is the nested typesize_type provided by such containers. It is usually defined as a synonym forstd::size_t.

      It is unspecified whether the declaration ofstd::size_t is available in any other standard library header. An implementation may avoid introducing this name even when the standard requiresstd::size_t to be used.

      Theinteger literal suffix forstd::size_t is any combination ofz orZ withu orU (i.e.zu,zU,Zu,ZU,uz,uZ,Uz, orUZ).

      (since C++23)

      [edit]Possible implementation

      using size_t= decltype(sizeof0);

      [edit]Example

      Run this code
      #include <array>#include <cstddef>#include <iostream> int main(){std::array<std::size_t,10> a; // Example with C++23 std::size_t literalfor(auto i= 0uz; i!= a.size();++i)std::cout<<(a[i]= i)<<' ';std::cout<<'\n'; // Example of decrementing loopfor(std::size_t i= a.size(); i--;)std::cout<< a[i]<<' ';std::cout<<'\n'; // Note the naive decrementing loop://  for (std::size_t i = a.size() - 1; i >= 0; --i) ...// is an infinite loop, because unsigned numbers are always non-negative}

      Output:

      0 1 2 3 4 5 6 7 8 99 8 7 6 5 4 3 2 1 0

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1122C++98std::size_t was circularly defined[1]it is implementation-defined
      CWG 1464C++98object size might be not representable instd::size_tsuch type is ill-formed
      1. The definition ofstd::size_t was exactly the same as the definition ofsize_t in C, which is “the result type ofsizeof”. There is no circular definition in C because the result type ofsizeof in C is an implementation-defined unsigned integer type.

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 6.8.4 Compound types [basic.compound] (p: 79-80)
      • 7.6.2.5 Sizeof [expr.sizeof] (p: 136)
      • 7.6.2.6 Alignof [expr.alignof] (p: 136)
      • 17.2.4 Sizes, alignments, and offsets [support.types.layout] (p: 504-505)
      • C++20 standard (ISO/IEC 14882:2020):
      • 6.8.3 Compound types [basic.compound] (p: 75-76)
      • 7.6.2.5 Sizeof [expr.sizeof] (p: 129-130)
      • 7.6.2.6 Alignof [expr.alignof] (p: 130)
      • 17.2.4 Sizes, alignments, and offsets [support.types.layout] (p: 507-508)
      • C++17 standard (ISO/IEC 14882:2017):
      • 6.9.2 Compound types [basic.compound] (p: 81-82)
      • 8.3.3 Sizeof [expr.sizeof] (p: 121-122)
      • 8.3.6 Alignof [expr.alignof] (p: 129)
      • 21.2.4 Sizes, alignments, and offsets [support.types.layout] (p: 479)
      • C++14 standard (ISO/IEC 14882:2014):
      • 3.9.2 Compound types [basic.compound] (p: 73-74)
      • 5.3.3 Sizeof [expr.sizeof] (p: 109-110)
      • 5.3.6 Alignof [expr.alignof] (p: 116)
      • 18.2 Types [support.types] (p: 443-444)
      • C++11 standard (ISO/IEC 14882:2011):
      • 5.3.3 Sizeof [expr.sizeof] (p: 111)
      • 5.3.6 Alignof [expr.alignof] (p: 116)
      • 18.2 Types [support.types] (p: 454-455)
      • C++03 standard (ISO/IEC 14882:2003):
      • 5.3.3 Sizeof [expr.sizeof] (p: 79)
      • C++98 standard (ISO/IEC 14882:1998):
      • 5.3.3 Sizeof [expr.sizeof] (p: 77)

      [edit]See also

      signed integer type returned when subtracting two pointers
      (typedef)[edit]
      byte offset from the beginning of astandard-layout type to specified member
      (function macro)[edit]
      integer literalsbinary,(since C++14) decimal, octal, or hexadecimal numbers of integer type[edit]
      C documentation forsize_t
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/size_t&oldid=183055"

      [8]ページ先頭

      ©2009-2025 Movatter.jp