Skip to content

Commit b9baf19

Browse files
derekmaurocopybara-github
authored andcommitted
Document the differing trimming behavior of absl::Span::subspan() and std::span::subspan()
`std::span::subspan()` has stricter preconditions than its `absl::` counterpart. Supplying a `len` that would extend beyond the end of the span is undefined behavior for `std::span` (unless `len` is the default `npos` value), whereas `absl::span` simply truncates the result. PiperOrigin-RevId: 836331418 Change-Id: I0e9a11cb434deca0b88d761e8233a44d5a9273ce
1 parent f33b7c0 commit b9baf19

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

absl/types/span.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
// * `absl::Span` has no static extent template parameter, nor constructors
4848
// which exist only because of the static extent parameter.
4949
// * `absl::Span` has an explicit mutable-reference constructor
50+
// * `absl::Span::subspan(pos, len)` always truncates `len` to
51+
// `size() - pos`, whereas `std::span::subspan()` only truncates when the
52+
// `len` parameter is defaulted.
5053
//
5154
// For more information, see the class comments below.
5255
#ifndef ABSL_TYPES_SPAN_H_
@@ -449,6 +452,11 @@ class ABSL_ATTRIBUTE_VIEW Span {
449452
// will be trimmed to at most size() - `pos`. A default `len` value of `npos`
450453
// ensures the returned subspan continues until the end of the span.
451454
//
455+
// Note that trimming behavior differs from `std::span::subspan()`.
456+
// `std::span::subspan()` requires `len == npos || pos + len <= size()`.
457+
// In other words, `std::span::subspan()` only trims `len` when its value is
458+
// defaulted.
459+
//
452460
// Examples:
453461
//
454462
// std::vector<int> vec = {10, 11, 12, 13};

0 commit comments

Comments
 (0)