std::size_t(3)
π NAME π
Okay so this is std::size_t β page std::size_t(3) from the C++ Standard Library manual, section 3, very official π
β and the NAME line says, and I quote, std::size_t - std::size_t. That's it. That's the name. Iconic, low-effort, ate. β¨
πΈ Synopsis πΈ
So bestie, this type is defined in header a whole bunch of places, like it's showing up to every party:
- 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>
And the actual declaration is this:
typedef /*implementation-defined*/ size_t;
Basically no cap: std::size_t is the unsigned integer type of the result of the sizeof operator, and also of the sizeof... operator and the alignof operator (since C++11). Three operators, one type, she's booked and busy π«Ά
Also the bit width of std::size_t is not less than 16. (since C++11) β so it's smol-ish minimum but never smaller than that, periodt.
π¦ Notes π¦
std::size_tcan store the maximum size of a theoretically possible object of any type (including array). A type whose size cannot be represented bystd::size_tis ill-formed π. On many platforms (an exception is systems with segmented addressing)std::size_tcan safely store the value of any non-member pointer, in which case it is synonymous withstd::uintptr_t. π₯std::size_tis commonly used for array indexing and loop counting. Programs that use other types, such asunsigned int, 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. π so like. use the right type bestie.- When indexing C++ containers, such as
std::string,std::vector, etc, the appropriate type is the member typedefsize_typeprovided by such containers. It is usually defined as a synonym forstd::size_t. Chef's kiss, consistent, we love a family resemblance π - It is unspecified whether the declaration of
std::size_tis available in any other standard library header. An implementation may avoid introducing this name even when the standard requiresstd::size_tto be used. π (translation: don't assume, check your headers) - The integer literal suffix for
std::size_tis any combination ofzor (since C++23)ZwithuorUβ i.e.zu,zU,Zu,ZU,uz,uZ,Uz, orUZ. Eight whole options, she's flexible π
π Possible implementation π
using size_t = decltype(sizeof(0)); // valid since C++11
Look at that. One line. Main character energy π
β¨ Example β¨
tiny note: the original page literally has a // Run this code link sitting above the snippet, and I'm passing that information along to you verbatim, bestie π§
#include <array>
#include <cstddef>
#include <iostream>
int main()
{
std::array<std::size_t, 10> a;
// Example with C++23 size_t literal
for (auto i = 0uz; i != a.size(); ++i)
std::cout << (a[i] = i) << ' ';
std::cout << '\n';
// Example of decrementing loop
for (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 9
9 8 7 6 5 4 3 2 1 0
That second loop is the vibe check passing, and that commented-out naive loop is the vibe check failing β unsigned numbers are always non-negative so i >= 0 is always true and you spiral forever ππ
π References π
Every single receipt, in order, not one skipped:
- C++23 standard (ISO/IEC 14882:2023):
6.8.4Compound types[basic.compound](p: 79-80)7.6.2.5Sizeof[expr.sizeof](p: 136)7.6.2.6Alignof[expr.alignof](p: 136)17.2.4Sizes, alignments, and offsets[support.types.layout](p: 504-505)
- C++20 standard (ISO/IEC 14882:2020):
6.8.3Compound types[basic.compound](p: 75-76)7.6.2.5Sizeof[expr.sizeof](p: 129-130)7.6.2.6Alignof[expr.alignof](p: 130)17.2.4Sizes, alignments, and offsets[support.types.layout](p: 507-508)
- C++17 standard (ISO/IEC 14882:2017):
6.9.2Compound types[basic.compound](p: 81-82)8.3.3Sizeof[expr.sizeof](p: 121-122)8.3.6Alignof[expr.alignof](p: 129)21.2.4Sizes, alignments, and offsets[support.types.layout](p: 479)
- C++14 standard (ISO/IEC 14882:2014):
3.9.2Compound types[basic.compound](p: 73-74)5.3.3Sizeof[expr.sizeof](p: 109-110)5.3.6Alignof[expr.alignof](p: 116)18.2Types[support.types](p: 443-444)
- C++11 standard (ISO/IEC 14882:2011):
5.3.3Sizeof[expr.sizeof](p: 111)5.3.6Alignof[expr.alignof](p: 116)18.2Types[support.types](p: 454-455)
- C++03 standard (ISO/IEC 14882:2003):
5.3.3Sizeof[expr.sizeof](p: 79)
- C++98 standard (ISO/IEC 14882:1998):
5.3.3Sizeof[expr.sizeof](p: 77)
π See also π
ptrdiff_tβ signed integer type returned when subtracting two pointers (typedef)offsetofβ byte offset from the beginning of a standard-layout type to specified member (function macro)integer literals(since C++14) β binary, decimal, octal, or hexadecimal numbers of integer type- C documentation for
size_t
Alright bestie, that's the whole page, nothing dropped, nothing invented π«Ά This one's from http://cppreference.com, dated 2024.06.10, and it lives at page std::size_t(3) in the manual β go forth and index your arrays correctly, mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms