你如何使用 std span 进行边界检查?

How do you do bounds checking with std span?

std::vector 和几乎所有其他容器都有一种非常方便的边界检查方式:at()std::span 显然没有。

相当笨拙,但像这样:

  1. 使用位置
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. 使用迭代器:
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;
}

将span引入标准库的paper说:

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.

也就是说,操作具有狭窄的契约,给予实施者自由。

如果您的标准库不允许您将行为控制到适当的粒度。 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(这可能正是您想要的)。