4 ms·
> "- Span For when you want to take an array or a vector." > How's this different from taking a pair of iterators? Safety and (a little) convenience. If your
by duneroadrunner 9y ago
> "- Span For when you want to take an array or a vector."
> How's this different from taking a pair of iterators?
Safety and (a little) convenience. If your function takes a pair of iterators, there's no way to ensure (at compile-time anyway) that both iterators are even pointing into the same array/vector. Also, spans can ensure, with bounds-checking, that only elements within a specified sub-range can be accessed.
Such bounds-enforcement is particularly important in cases like, for example, SaferCPlusPlus' "TRASectionSplitter", which is a data type that allows you to partition an array/vector/whatever into subsections that can each be safely accessed/modified concurrently from different threads. ("TRASectionSplitter" is not yet documented, but for those interested, example code can be found here[1].)
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus/blob/master/msetl_example.cpp#L1903 https://github.com/duneroadrunner/SaferCPlusPlus/blob/master...