I need to be able to access (read-only, no resizing involved or anything like that) the elements of a an std::vector
via pointers. E.g.,
std::vector<int> foo(10);
int *ptr_begin = &foo[0];
So far so good, this is guaranteed to work in the current standard (23.3.6.1):
The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().
So we can access all elements of the vector using pointers, since they are stored in a contiguous chunk of memory. But what about the one-past-the-last element? I mean, it is legal to perform this operation?
int *ptr_end = ptr_begin + foo.size()
(Note, I am not trying to access the past-the-last value, merely to define a pointer to it - an equivalent to foo.end()
if you like). The standard mentions only accessing elements via pointer arithmetics, but clearly here we are not accessing any element.
(As a side note, the definition of the existence of a past-the-last something seems tightly connected to the base concept of array (see for instance 5.7/5), but throughout the standard it seems like the concepts of contiguous storage and array are used interchangeably. Am I reading wrong?)