What guarantees does std::span provide as a non-owning contiguous view, and what lifetime and bounds risks remain?
Show answer and explanation
std::span represents a contiguous sequence as a pointer plus an extent that is either compile-time fixed or runtime dynamic. It does not own storage or extend the source lifetime, so returning a span to local data or retaining one across vector reallocation creates a dangling view. In C++20, operator[] requires the index to be in range and does not provide throwing bounds checks; callers must validate indices and subview preconditions. Span is ideal for APIs that borrow arrays, vectors, or other contiguous ranges for the duration of a call while making length explicit.
std::span represents a contiguous sequence as a pointer plus an extent that is either compile-time fixed or runtime dynamic. It does not own storage or extend the source lifetime, so returning a span to local data or retaining one across vector reallocation creates a dangling view. In C++20, operator[] requires the index to be in range and does not provide throwing bounds checks; callers must validate indices and subview preconditions. Span is ideal for APIs that borrow arrays, vectors, or other contiguous ranges for the duration of a call while making length explicit.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.