Triggered by this question I was reading about out of bounds access via std::span::operator[] here:
If
idx < size()isfalse, the behavior is undefined.(until C++26)
Why is it specified explicitly to be undefined?
With
std::span s{ptr,size};
I see two cases:
The underlying array has only
s.size()elements, attempting to calls[s.size()]is undefined, becauseptr[size]is undefined. Thatspan::operator[]is specified to be undefined does not actually change that.The underlying array has more than
s.size()elements. In that cases.data()+sizecan be dereferenced without problem, yets[s.size()]is undefined.
What is the rationale of making span::operator undefined for elements that can be reached via span::data() ?