addressing feedback

This commit is contained in:
Jordan Maples [MSFT] 2020-02-03 18:12:57 -08:00
parent 1815791af8
commit 81c2a1da15

View File

@ -333,7 +333,7 @@ namespace details
public: public:
using index_type = std::size_t; using index_type = std::size_t;
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size."); static_assert(Ext != dynamic_extent, "A fixed-size span must be >= 0 in size.");
constexpr extent_type() noexcept {} constexpr extent_type() noexcept {}
@ -417,7 +417,7 @@ public:
constexpr span(pointer ptr, index_type count) noexcept : storage_(ptr, count) {} constexpr span(pointer ptr, index_type count) noexcept : storage_(ptr, count) {}
constexpr span(pointer firstElem, pointer lastElem) noexcept constexpr span(pointer firstElem, pointer lastElem) noexcept
: storage_(firstElem, std::distance(firstElem, lastElem)) : storage_(firstElem, static_cast<std::size_t>(std::distance(firstElem, lastElem)))
{} {}
template <std::size_t N> template <std::size_t N>
@ -536,7 +536,7 @@ public:
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr reference operator[](index_type idx) const noexcept constexpr reference operator[](index_type idx) const noexcept
{ {
Expects(CheckRange(idx, storage_.size())); Expects(idx < size());
return data()[idx]; return data()[idx];
} }
@ -594,32 +594,6 @@ public:
#endif // _MSC_VER #endif // _MSC_VER
private: private:
static constexpr bool CheckRange(index_type idx, index_type size) noexcept
{
// Optimization:
//
// idx >= 0 && idx < size
// =>
// static_cast<size_t>(idx) < static_cast<size_t>(size)
//
// because size >=0 by span construction, and negative idx will
// wrap around to a value always greater than size when casted.
// check if we have enough space to wrap around
#if defined(__cpp_if_constexpr)
if constexpr (sizeof(index_type) <= sizeof(size_t))
#else
if (sizeof(index_type) <= sizeof(size_t))
#endif
{
return narrow_cast<size_t>(idx) < narrow_cast<size_t>(size);
}
else
{
return idx < size;
}
}
// Needed to remove unnecessary null check in subspans // Needed to remove unnecessary null check in subspans
struct KnownNotNull struct KnownNotNull
{ {