From f4c608fd39fe8129dfb2ea5e737e2aea6baf4519 Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 18 Feb 2020 14:09:11 -0800 Subject: [PATCH] addressing comments --- include/gsl/span | 47 ++++++++++++++---------------- tests/span_compatibility_tests.cpp | 12 ++++---- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index cffe043..e3ea0db 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -25,7 +25,6 @@ #include // for array #include // for ptrdiff_t, size_t, nullptr_t #include // for reverse_iterator, distance, random_access_... -#include // for std::addressof #include #include // for enable_if_t, declval, is_convertible, inte... #include @@ -147,14 +146,14 @@ namespace details constexpr reference operator*() const noexcept { - Expects(begin_ && current_ && end_); + Expects(begin_ && end_); Expects(begin_ <= current_ && current_ < end_); return *current_; } constexpr pointer operator->() const noexcept { - Expects(begin_ && current_ && end_); + Expects(begin_ && end_); Expects(begin_ <= current_ && current_ < end_); return current_; } @@ -175,7 +174,7 @@ namespace details constexpr span_iterator& operator--() noexcept { - Expects(begin_ && current_ && end_); + Expects(begin_ && end_); Expects(begin_ < current_); --current_; return *this; @@ -346,7 +345,7 @@ namespace details static_assert(Ext != dynamic_extent, "A fixed-size span must not have size == dynamic_extent"); - constexpr extent_type() noexcept {} + constexpr extent_type() noexcept = default; template constexpr extent_type(extent_type ext) @@ -433,7 +432,7 @@ public: } constexpr span(pointer firstElem, pointer lastElem) noexcept - : storage_(firstElem, static_cast(std::distance(firstElem, lastElem))) + : storage_(firstElem, static_cast(lastElem - firstElem)) { if (Extent != dynamic_extent) { Expects(lastElem - firstElem == static_cast(Extent)); } @@ -442,7 +441,7 @@ public: template ::value, int> = 0> constexpr span(element_type (&arr)[N]) noexcept - : storage_(KnownNotNull{std::addressof(arr[0])}, details::extent_type()) + : storage_(KnownNotNull{arr + 0}, details::extent_type()) {} template ::value && !details::is_std_array::value && - std::is_convertible::value && - std::is_convertible().data()), pointer>::value>> + std::is_pointer().data())>::value && + std::is_convertible().data())>(*)[], element_type(*)[]>::value>> constexpr span(Container& cont) noexcept : span(cont.data(), cont.size()) {} @@ -474,8 +473,8 @@ public: class = std::enable_if_t< std::is_const::value && !details::is_span::value && !details::is_std_array::value && - std::is_convertible::value && - std::is_convertible().data()), pointer>::value>> + std::is_pointer().data())>::value && + std::is_convertible().data())>(*)[], element_type(*)[]>::value>> constexpr span(const Container& cont) noexcept : span(cont.data(), cont.size()) {} @@ -497,7 +496,6 @@ public: template constexpr span first() const noexcept { - Expects(Count != dynamic_extent); Expects(Count <= size()); return {data(), Count}; } @@ -505,20 +503,19 @@ public: template // clang-format off GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - // clang-format on - constexpr span last() const noexcept + // clang-format on + constexpr span last() const noexcept { - Expects(Count != dynamic_extent); - Expects(size() >= Count); + Expects(Count <= size()); return {data() + (size() - Count), Count}; } template // clang-format off GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - // clang-format on - constexpr auto subspan() const noexcept -> - typename details::calculate_subspan_type::type + // clang-format on + constexpr auto subspan() const noexcept -> + typename details::calculate_subspan_type::type { Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset))); @@ -589,8 +586,8 @@ public: constexpr iterator end() const noexcept { const auto data = storage_.data(); - const auto size = storage_.size(); - return {data, data + size, data + size}; + const auto endData = data + storage_.size(); + return {data, endData, endData}; } constexpr const_iterator cbegin() const noexcept @@ -602,8 +599,8 @@ public: constexpr const_iterator cend() const noexcept { const auto data = storage_.data(); - const auto size = storage_.size(); - return {data, data + size, data + size}; + const auto endData = data + storage_.size(); + return {data, endData, endData}; } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } @@ -767,6 +764,7 @@ namespace details template struct calculate_byte_size : std::integral_constant { + static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big."); }; template @@ -843,12 +841,11 @@ constexpr span make_span(Ptr& cont) return span(cont); } -// Specialization of gsl::at for span template constexpr ElementType& at(span s, index i) { // No bounds checking here because it is done in span::operator[] called below - Ensures(i >= 0); + Expects(i >= 0); return s[static_cast(i)]; } diff --git a/tests/span_compatibility_tests.cpp b/tests/span_compatibility_tests.cpp index 57a85e7..0301ace 100644 --- a/tests/span_compatibility_tests.cpp +++ b/tests/span_compatibility_tests.cpp @@ -931,6 +931,8 @@ static_assert(!std::is_constructible, Derived (&)[3]>::value, "!std::is_constructible, Derived(&)[3]>"); static_assert(!std::is_constructible, std::array&>::value, "!std::is_constructible, std::array&>"); +static_assert(!std::is_constructible, std::vector&>::value, + "!std::is_constructible, std::vector&>"); static_assert(!std::is_constructible, const gsl::span&>::value, "!std::is_constructible, const gsl::span&>"); static_assert(!std::is_constructible, const gsl::span&>::value, @@ -1048,16 +1050,16 @@ static_assert(std::is_convertible&, gsl::span>::va static_assert(std::is_convertible&, gsl::span>::value, "std::is_convertible&, gsl::span>"); - - + + #if __cplusplus >= 201703l template -inline constexpr bool AsWritableBytesCompilesFor = false; +static constexpr bool AsWritableBytesCompilesFor = false; template -inline constexpr bool AsWritableBytesCompilesFor()))>> = +static constexpr bool AsWritableBytesCompilesFor()))>> = true; - + static_assert(AsWritableBytesCompilesFor>, "AsWritableBytesCompilesFor>"); static_assert(AsWritableBytesCompilesFor>,