diff --git a/libcxx/include/span b/libcxx/include/span --- a/libcxx/include/span +++ b/libcxx/include/span @@ -307,13 +307,13 @@ _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept { - static_assert(_Extent > 0, "span[].front() on empty span"); + _LIBCPP_ASSERT(!empty(), "span::front() on empty span"); return __data[0]; } _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept { - static_assert(_Extent > 0, "span[].back() on empty span"); + _LIBCPP_ASSERT(!empty(), "span::back() on empty span"); return __data[size()-1]; } diff --git a/libcxx/test/std/containers/views/span.elem/back.pass.cpp b/libcxx/test/std/containers/views/span.elem/back.pass.cpp --- a/libcxx/test/std/containers/views/span.elem/back.pass.cpp +++ b/libcxx/test/std/containers/views/span.elem/back.pass.cpp @@ -30,7 +30,6 @@ return std::addressof(sp.back()) == sp.data() + sp.size() - 1; } - template void testRuntimeSpan(Span sp) { @@ -38,6 +37,12 @@ assert(std::addressof(sp.back()) == sp.data() + sp.size() - 1); } +template +void testEmptySpan(Span sp) +{ + if (!sp.empty()) + [[maybe_unused]] auto res = sp.back(); +} struct A{}; constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; @@ -71,5 +76,8 @@ testRuntimeSpan(std::span (&s, 1)); testRuntimeSpan(std::span(&s, 1)); + std::span sp; + testEmptySpan(sp); + return 0; } diff --git a/libcxx/test/std/containers/views/span.elem/front.pass.cpp b/libcxx/test/std/containers/views/span.elem/front.pass.cpp --- a/libcxx/test/std/containers/views/span.elem/front.pass.cpp +++ b/libcxx/test/std/containers/views/span.elem/front.pass.cpp @@ -38,6 +38,12 @@ assert(std::addressof(sp.front()) == sp.data()); } +template +void testEmptySpan(Span sp) +{ + if (!sp.empty()) + [[maybe_unused]] auto res = sp.front(); +} struct A{}; constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; @@ -71,5 +77,8 @@ testRuntimeSpan(std::span (&s, 1)); testRuntimeSpan(std::span(&s, 1)); + std::span sp; + testEmptySpan(sp); + return 0; }