size_t is unsigned, but built-in subscripts can be negative

like title, is that size_t just for built-in arrays? cause pointers can use negative index.
when you're indexing a pointer using the p[n] notation, it behaves as if you're using pointer arithmetic *(p + n).

Pointer arithmetic is not really defined for std::size_t parameters, it is defined semantically for any "integral type" (5.7[expr.add]p4), and for the purpose of overload resolution, for std::ptrdiff_t parameters: it is T* operator+(T*, std::ptrdiff_t) (13.6[over.built]p13)
Last edited on
thx, Cubbi
do you mean that size_t only for arrays not for pointer index, pointer index is of type ptrdiff_t?
size_t is the size of the array (the result of sizeof).
ptrdiff_t is the distance between two elements of an array (the result of subtracting two pointers)
if your array is bigger than PTRDIFF_MAX, certain things (such as subtracting two pointers) cause undefined behavior
thx alot
Topic archived. No new replies allowed.