Support C++20 contiguous iterators in absl::MakeSpan and MakeConstSpan.
Callers frequently construct spans from container iterators by writing
`absl::MakeSpan(&*it1, &*it2)` or `absl::MakeSpan(&*it, len)`. Dereferencing
an end iterator is undefined behavior—and traps under hardened/bounded
iterators (such as `libc++`'s `__bounded_iter`). Rewriting those calls to drop
down to `c.data() + ...` is verbose and throws away iterator bounds checking.
The cleanest and safest rewrite is `absl::MakeSpan(it1, it2)` or
`absl::MakeSpan(it, len)`. This lets callers keep any offset arithmetic
(`it + n`, `end - k`) in iterator space, where bounded iterators check it,
instead of converting to raw pointers first and doing unchecked pointer
arithmetic.
As a backstop, the new overloads also validate the range themselves: the
`(first, length)` form checks that `first + length` is in bounds, and the
`(first, last)` form checks that `last - first` is non-negative and that
`last` lies within `first`'s bounds (catching mismatched containers).
Specifically, under `ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L` in
`absl/types/span.h`:
- Add `absl::MakeSpan` and `absl::MakeConstSpan` overloads taking a
`std::contiguous_iterator` with a size or a sized sentinel. Raw pointers
keep using the existing overloads.
- Validate `length <= max(iter_difference_t)` / `last - first >= 0`, and
compute `first + <span length>` so bounded iterators trap on out-of-bounds
ranges. The data pointer is taken with `std::to_address(first)`, which is
valid even for an end iterator.
- Mark the existing pointer-pair `MakeSpan` and `MakeConstSpan` overloads
`constexpr`.
`absl::Span` itself gains no constructors. An iterator-pair constructor makes
existing calls such as `f({it1, it2})` ambiguous when `f` is overloaded on
`Span` and a container, and compilers disagree on whether marking it
`explicit` avoids that.
PiperOrigin-RevId: 1003354434
diff --git a/absl/types/span.h b/absl/types/span.h
index 96cbef2..4b9fb60 100644
--- a/absl/types/span.h
+++ b/absl/types/span.h
@@ -96,6 +96,10 @@
// NOLINTNEXTLINE(build/c++20)
inline constexpr bool std::ranges::enable_borrowed_range<absl::Span<T>> = true;
#endif
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+#include <limits>
+#include <memory>
+#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
@@ -712,6 +716,12 @@
// return absl::MakeSpan(&array[0], num_elements_);
// }
//
+// In C++20 and later, `MakeSpan()` and `MakeConstSpan()` also accept a
+// contiguous iterator together with a size or a sized sentinel, e.g.
+// `absl::MakeSpan(v.begin() + 1, v.end())`. Prefer this to
+// `absl::MakeSpan(&*first, &*last)`, which dereferences `last`. With
+// bounds-checked iterators, an out-of-bounds range is a hardening failure.
+//
// NOTE: To avoid undefined behavior if the container is empty, use `.data()`
// or pass the container directly instead of using `&v[0]` or `&v[v.size()]`.
//
@@ -721,13 +731,49 @@
return Span<T>(ptr, size);
}
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+template <int&... ExplicitArgumentBarrier, std::contiguous_iterator It>
+ requires(!std::is_pointer_v<It>)
+constexpr Span<std::remove_reference_t<std::iter_reference_t<It>>> MakeSpan(
+ It first, size_t size) {
+ absl::base_internal::HardeningAssertLE(
+ size, static_cast<size_t>(
+ (std::numeric_limits<std::iter_difference_t<It>>::max)()));
+ // Compute `first + size` so bounded iterators trap if
+ // `[first, first + size)` is out of bounds. `std::to_address` is valid even
+ // for an end iterator.
+ (void)(first + static_cast<std::iter_difference_t<It>>(size));
+ return Span<std::remove_reference_t<std::iter_reference_t<It>>>(
+ std::to_address(first), size);
+}
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+
template <int&... ExplicitArgumentBarrier, typename T>
-Span<T> MakeSpan(T* absl_nullable begin ABSL_ATTRIBUTE_LIFETIME_BOUND,
- T* absl_nullable end) noexcept {
+constexpr Span<T> MakeSpan(T* absl_nullable begin ABSL_ATTRIBUTE_LIFETIME_BOUND,
+ T* absl_nullable end) noexcept {
absl::base_internal::HardeningAssertLE(begin, end);
return Span<T>(begin, static_cast<size_t>(end - begin));
}
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+template <int&... ExplicitArgumentBarrier, std::contiguous_iterator It,
+ std::sized_sentinel_for<It> End>
+ requires(!std::is_convertible_v<End, size_t> &&
+ !(std::is_pointer_v<It> && std::is_pointer_v<End>))
+constexpr Span<std::remove_reference_t<std::iter_reference_t<It>>> MakeSpan(
+ It first, End last) {
+ const auto dist = last - first;
+ absl::base_internal::HardeningAssertGE(dist, 0);
+ // Backstop for bounded iterators: computing `first + dist` traps if `last`
+ // is outside `first`'s bounds, e.g. it came from a different container or
+ // is a custom sentinel. Same-container ranges were checked by the caller's
+ // own iterator arithmetic.
+ (void)(first + dist);
+ return Span<std::remove_reference_t<std::iter_reference_t<It>>>(
+ std::to_address(first), static_cast<size_t>(dist));
+}
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+
template <int&... ExplicitArgumentBarrier, typename C>
constexpr auto MakeSpan(C& c) noexcept // NOLINT(runtime/references)
-> std::enable_if_t<span_internal::IsView<C>::value,
@@ -781,14 +827,34 @@
return Span<const T>(ptr, size);
}
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+template <int&... ExplicitArgumentBarrier, std::contiguous_iterator It>
+ requires(!std::is_pointer_v<It>)
+constexpr Span<const std::remove_reference_t<std::iter_reference_t<It>>>
+MakeConstSpan(It first, size_t size) {
+ return absl::MakeSpan(first, size);
+}
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+
template <int&... ExplicitArgumentBarrier, typename T>
-Span<const T> MakeConstSpan(T* absl_nullable begin
- ABSL_ATTRIBUTE_LIFETIME_BOUND,
- T* absl_nullable end) noexcept {
+constexpr Span<const T> MakeConstSpan(T* absl_nullable begin
+ ABSL_ATTRIBUTE_LIFETIME_BOUND,
+ T* absl_nullable end) noexcept {
absl::base_internal::HardeningAssertLE(begin, end);
return Span<const T>(begin, end - begin);
}
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+template <int&... ExplicitArgumentBarrier, std::contiguous_iterator It,
+ std::sized_sentinel_for<It> End>
+ requires(!std::is_convertible_v<End, size_t> &&
+ !(std::is_pointer_v<It> && std::is_pointer_v<End>))
+constexpr Span<const std::remove_reference_t<std::iter_reference_t<It>>>
+MakeConstSpan(It first, End last) {
+ return absl::MakeSpan(first, last);
+}
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+
template <int&... ExplicitArgumentBarrier, typename C>
constexpr auto MakeConstSpan(const C& c) noexcept
-> std::enable_if_t<
diff --git a/absl/types/span_test.cc b/absl/types/span_test.cc
index c814726..fdb9bd5 100644
--- a/absl/types/span_test.cc
+++ b/absl/types/span_test.cc
@@ -18,6 +18,7 @@
#include <array>
#include <cstddef>
#include <initializer_list>
+#include <iterator>
#include <numeric>
#include <stdexcept>
#include <string>
@@ -91,6 +92,31 @@
EXPECT_THAT(s, SpanIs(a, 2));
}
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+template <typename It>
+class SizedSentinel {
+ public:
+ SizedSentinel() = default;
+ explicit constexpr SizedSentinel(It end) : end_(end) {}
+ friend constexpr bool operator==(It it, SizedSentinel s) {
+ return it == s.end_;
+ }
+ friend constexpr std::iter_difference_t<It> operator-(SizedSentinel s,
+ It it) {
+ return s.end_ - it;
+ }
+ friend constexpr std::iter_difference_t<It> operator-(It it,
+ SizedSentinel s) {
+ return it - s.end_;
+ }
+
+ private:
+ It end_{};
+};
+template <typename It>
+SizedSentinel(It) -> SizedSentinel<It>;
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+
TEST(IntSpan, ArrayCtor) {
int a[] = {1, 2, 3};
absl::Span<int> s(a);
@@ -383,6 +409,49 @@
EXPECT_THAT(absl::MakeConstSpan(v.data(), v.data() + 1), SpanIs(s));
}
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+TEST(IntSpan, MakeSpanContiguousIterators) {
+ std::vector<int> empty;
+ auto s_empty_len = absl::MakeSpan(empty.begin(), empty.size());
+ EXPECT_THAT(s_empty_len, SpanIs(empty));
+ auto s_empty_end = absl::MakeSpan(empty.begin(), empty.end());
+ EXPECT_THAT(s_empty_end, SpanIs(empty));
+
+ std::vector<int> v{1, 2, 3};
+ auto s_len = absl::MakeSpan(v.begin(), 2);
+ EXPECT_THAT(s_len, SpanIs(v.data(), 2));
+ auto s_end = absl::MakeSpan(v.begin(), v.begin() + 2);
+ EXPECT_THAT(s_end, SpanIs(v.data(), 2));
+ auto s_cend = absl::MakeSpan(v.begin(), v.cend());
+ EXPECT_THAT(s_cend, SpanIs(v));
+ auto s_sentinel = absl::MakeSpan(v.begin(), SizedSentinel(v.end()));
+ EXPECT_THAT(s_sentinel, SpanIs(v));
+
+ EXPECT_THAT(absl::MakeConstSpan(empty.begin(), empty.size()),
+ SpanIs(s_empty_len));
+ EXPECT_THAT(absl::MakeConstSpan(empty.begin(), empty.end()),
+ SpanIs(s_empty_end));
+ EXPECT_THAT(absl::MakeConstSpan(v.begin(), 2), SpanIs(s_len));
+ EXPECT_THAT(absl::MakeConstSpan(v.begin(), v.begin() + 2), SpanIs(s_end));
+ EXPECT_THAT(absl::MakeConstSpan(v.begin(), v.cend()), SpanIs(v));
+ EXPECT_THAT(absl::MakeConstSpan(v.begin(), SizedSentinel(v.end())),
+ SpanIs(v));
+
+#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
+ EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(v.end(), v.begin()), "");
+ EXPECT_DEATH_IF_SUPPORTED(absl::MakeConstSpan(v.end(), v.begin()), "");
+ EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(v.begin(), size_t(-1)), "");
+ EXPECT_DEATH_IF_SUPPORTED(absl::MakeConstSpan(v.begin(), size_t(-1)), "");
+#endif
+#if defined(_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR) && \
+ (!defined(NDEBUG) || ABSL_OPTION_HARDENED)
+ // With bounded iterators, a length past the end of the container traps.
+ EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(v.begin(), 4), "");
+ EXPECT_DEATH_IF_SUPPORTED(absl::MakeConstSpan(v.begin(), 4), "");
+#endif
+}
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+
TEST(IntSpan, MakeSpanContainer) {
std::vector<int> empty;
auto s_empty = absl::MakeSpan(empty);
@@ -454,6 +523,17 @@
CheckType<absl::Span<const int>>(absl::MakeSpan(ca, 1));
CheckType<absl::Span<char>>(absl::MakeSpan(s));
CheckType<absl::Span<const char>>(absl::MakeSpan(cs));
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+ CheckType<absl::Span<int>>(absl::MakeSpan(vec.begin(), vec.end()));
+ CheckType<absl::Span<int>>(absl::MakeSpan(vec.begin(), vec.cend()));
+ CheckType<absl::Span<int>>(absl::MakeSpan(vec.begin(), 0));
+ CheckType<absl::Span<const int>>(absl::MakeSpan(cvec.begin(), cvec.end()));
+ CheckType<absl::Span<const int>>(absl::MakeSpan(cvec.begin(), 0));
+ CheckType<absl::Span<char>>(absl::MakeSpan(s.begin(), s.end()));
+ CheckType<absl::Span<char>>(absl::MakeSpan(s.begin(), 0));
+ CheckType<absl::Span<const char>>(absl::MakeSpan(cs.begin(), cs.end()));
+ CheckType<absl::Span<const char>>(absl::MakeSpan(cs.begin(), 0));
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
}
TEST(ConstIntSpan, MakeConstSpanTypes) {
@@ -490,6 +570,19 @@
CheckType<absl::Span<const int>>(absl::MakeConstSpan(shallow_container));
CheckType<absl::Span<const int>>(
absl::MakeConstSpan(const_shallow_container));
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+ CheckType<absl::Span<const int>>(absl::MakeConstSpan(vec.begin(), vec.end()));
+ CheckType<absl::Span<const int>>(
+ absl::MakeConstSpan(vec.begin(), vec.cend()));
+ CheckType<absl::Span<const int>>(absl::MakeConstSpan(vec.begin(), 0));
+ CheckType<absl::Span<const int>>(
+ absl::MakeConstSpan(cvec.begin(), cvec.end()));
+ CheckType<absl::Span<const int>>(absl::MakeConstSpan(cvec.begin(), 0));
+ CheckType<absl::Span<const char>>(absl::MakeConstSpan(s.begin(), s.end()));
+ CheckType<absl::Span<const char>>(absl::MakeConstSpan(s.begin(), 0));
+ CheckType<absl::Span<const char>>(absl::MakeConstSpan(cs.begin(), cs.end()));
+ CheckType<absl::Span<const char>>(absl::MakeConstSpan(cs.begin(), 0));
+#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
}
TEST(IntSpan, Equality) {
@@ -839,6 +932,17 @@
ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(&a[0], 1));
ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(c));
ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(a));
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+ static constexpr std::array<int, 2> std_arr = {1, 2};
+ ABSL_TEST_CONSTEXPR(absl::MakeSpan(std_arr.begin(), 1));
+ ABSL_TEST_CONSTEXPR(absl::MakeSpan(std_arr.begin(), std_arr.end()));
+ ABSL_TEST_CONSTEXPR(
+ absl::MakeSpan(std_arr.begin(), SizedSentinel(std_arr.end())));
+ ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(std_arr.begin(), 1));
+ ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(std_arr.begin(), std_arr.end()));
+ ABSL_TEST_CONSTEXPR(
+ absl::MakeConstSpan(std_arr.begin(), SizedSentinel(std_arr.end())));
+#endif
constexpr absl::Span<const int> span = c;
ABSL_TEST_CONSTEXPR(span.data());