Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

How do you do bounds checking with std span?

std::vector and pretty much all other containers have a very convenient way of bounds checking: at(). std::span doesn't have that apparently.

Upvotes: 6

Views: 2323

Answers (3)

ecatmur
ecatmur

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

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

std::span::at will be added in C++26.

Upvotes: 2

Alberto
Alberto

Reputation: 12949

Pretty clunky but something like this:

  1. using position
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];
}
  1. using iterators:
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

Related Questions