Reputation: 29985
std::vector
and pretty much all other containers have a very convenient way of bounds checking: at()
. std::span
doesn't have that apparently.
at()
?Upvotes: 6
Views: 2323
Reputation: 157444
The paper that introduced span to the standard library says:
Range-checking and bounds-safety
All accesses to the data encapsulated by a span are conceptually range-checked to ensure they remain within the bounds of the span. What actually happens as the result of a failure to meet span’s boundssafety constraints at runtime is undefined behavior.
That is, the operations have narrow contracts, giving freedom to the implementer.
If your standard library doesn't let you control the behavior to the appropriate granularity. gsl-lite offers drop-in replacement with configurable contract violation behavior. The Microsoft GSL was previously configurable, but now always terminates on contract violation, discussed here (which might actually be what you want).
Upvotes: 2
Reputation: 12949
Pretty clunky but something like this:
template<class Container>
auto& at(Container&& c, std::size_t pos){
if(pos >= c.size())
throw std::out_of_range("out of bounds");
return c[pos];
}
template<class Iterator, class Container>
auto& at(Container&& c, Iterator&& it){
if(std::distance(c.begin(), it) >= c.size())
throw std::out_of_range("out of bounds");
return *it;
}
Upvotes: 2