My understanding of std::span
is that it is essentially contains pointer into a container, a size, and some useful member functions.
template<typename T>
class SimpleSpan {
T* ptr;
size_t length;
// some member functions
}
I can take a span of a vector as seen in this question.
If I add entries to the end of a vector, the vector may need to be resized. When the vector is resized, the following steps occur (order not important).
A new array is allocated on the heap with more space.
The entries from the vector are moved to the new array.
The member
ptr
of thevector
to thevector
's array is changed to the start of the new array.The old array is deallocated.
Does anything happen to a span
if the vector
's array needs to be reallocated and made larger? Is using a span
into a vector
after push_back
is called on the vector
undefined behavior?