MemSpan is a view on a contiguous section of writeable memory. A view does not own the memory
and neither allocates nor de-allocates. The memory in the view is always owned by some other container
and it is the responsibility of the code to make sure the lifetime of the view is no more than that
of the owning container 1.
MemSpan is generally constructed on either an array or an allocated buffer. This allows
the buffer to be passed around with intrinsic length information. The buffer can also be treated as
an array of varying types, which makes working with serialized data much easier.
A span of writable memory. Because this is a chunk of memory, conceptually delimited by start and end pointers, the sizing type is
ptrdiff_tso that all of the sizing is consistent with differences between pointers. The memory is owned by some other object and that object must maintain the memory as long as the span references it.
Return a pointer to the first byte of the span.
operator==(MemSpan const &that) const¶
Check the equality of two spans, which are equal if they contain the same number of bytes of the same values.
is_same(MemSpan const &that) const¶
Check if that is the same span as this, that is the spans contain the exact same bytes.
at(ptrdiff_t n) const¶
Return a value of type V as if the span were are array of type V.
ptr(ptrdiff_t n) const¶
Return a pointer to a value of type V as if the span were are array of type V.
prefix(ptrdiff_t n) const¶
Return a new instance that contains the first n bytes of the current span. If n is larger than the number of bytes in the span, only that many bytes are returned.
- void *
Strong caution must be used with containers such as
std::stringbecause the lifetime of the memory can be much less than the lifetime of the container. In particular, adding or removing any element from a
std::vectorcan cause a re-allocation, invalidating any view of the original memory. In general views should be treated like iterators, suitable for passing to nested function calls but not for storing.