Commit d93ae920 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Simplify static_asserts in span's subview API

This change Simplifies the static_asserts in base::span's fixed subview
API first<Count>(), last<Count>() and subspan<Count, Offset>(). These
simplifications rely on the fact that dynamic_extent is equal to
std::numeric_limits<size_t>::max(), and thus will never be smaller than
any other possible value.

References:
- https://cplusplus.github.io/LWG/issue3103
- https://github.com/cplusplus/draft/commit/6a0aa7

Bug: 828324
Change-Id: I626b7e65b1ae8010485c4387a936704810e43fe5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2089843
Auto-Submit: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747494}
parent a50e0507
...@@ -309,16 +309,14 @@ class span : public internal::ExtentStorage<Extent> { ...@@ -309,16 +309,14 @@ class span : public internal::ExtentStorage<Extent> {
// [span.sub], span subviews // [span.sub], span subviews
template <size_t Count> template <size_t Count>
constexpr span<T, Count> first() const noexcept { constexpr span<T, Count> first() const noexcept {
static_assert(Extent == dynamic_extent || Count <= Extent, static_assert(Count <= Extent, "Count must not exceed Extent");
"Count must not exceed Extent");
CHECK(Extent != dynamic_extent || Count <= size()); CHECK(Extent != dynamic_extent || Count <= size());
return {data(), Count}; return {data(), Count};
} }
template <size_t Count> template <size_t Count>
constexpr span<T, Count> last() const noexcept { constexpr span<T, Count> last() const noexcept {
static_assert(Extent == dynamic_extent || Count <= Extent, static_assert(Count <= Extent, "Count must not exceed Extent");
"Count must not exceed Extent");
CHECK(Extent != dynamic_extent || Count <= size()); CHECK(Extent != dynamic_extent || Count <= size());
return {data() + (size() - Count), Count}; return {data() + (size() - Count), Count};
} }
...@@ -330,10 +328,8 @@ class span : public internal::ExtentStorage<Extent> { ...@@ -330,10 +328,8 @@ class span : public internal::ExtentStorage<Extent> {
: (Extent != dynamic_extent ? Extent - Offset : (Extent != dynamic_extent ? Extent - Offset
: dynamic_extent))> : dynamic_extent))>
subspan() const noexcept { subspan() const noexcept {
static_assert(Extent == dynamic_extent || Offset <= Extent, static_assert(Offset <= Extent, "Offset must not exceed Extent");
"Offset must not exceed Extent"); static_assert(Count == dynamic_extent || Count <= Extent - Offset,
static_assert(Extent == dynamic_extent || Count == dynamic_extent ||
Count <= Extent - Offset,
"Count must not exceed Extent - Offset"); "Count must not exceed Extent - Offset");
CHECK(Extent != dynamic_extent || Offset <= size()); CHECK(Extent != dynamic_extent || Offset <= size());
CHECK(Extent != dynamic_extent || Count == dynamic_extent || CHECK(Extent != dynamic_extent || Count == dynamic_extent ||
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment