change span from signed to unsigned to align to std

This commit is contained in:
Jordan Maples [MSFT] 2020-02-03 10:56:31 -08:00
parent b4dd39615a
commit 8579165d0a
3 changed files with 212 additions and 193 deletions

View File

@ -26,16 +26,18 @@
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t #include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <iterator> // for reverse_iterator, distance, random_access_... #include <iterator> // for reverse_iterator, distance, random_access_...
#include <limits> #include <limits>
#include <memory> // for std::addressof
#include <stdexcept> #include <stdexcept>
#include <type_traits> // for enable_if_t, declval, is_convertible, inte... #include <type_traits> // for enable_if_t, declval, is_convertible, inte...
#include <utility> #include <utility>
#include <memory> // for std::addressof
#if defined(_MSC_VER) && !defined(__clang__) #if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(push) #pragma warning(push)
// turn off some warnings that are noisy about our Expects statements // turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant #pragma warning(disable : 4127) // conditional expression is constant
#pragma warning( \
disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
#pragma warning(disable : 4702) // unreachable code #pragma warning(disable : 4702) // unreachable code
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool. // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
@ -70,9 +72,9 @@ namespace gsl
{ {
// [views.constants], constants // [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1; constexpr const std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> template <class ElementType, std::size_t Extent = dynamic_extent>
class span; class span;
// implementation details // implementation details
@ -83,7 +85,7 @@ namespace details
{ {
}; };
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
{ {
}; };
@ -108,7 +110,7 @@ namespace details
{ {
}; };
template <std::ptrdiff_t From, std::ptrdiff_t To> template <std::size_t From, std::size_t To>
struct is_allowed_extent_conversion struct is_allowed_extent_conversion
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent || : public std::integral_constant<bool, From == To || From == gsl::dynamic_extent ||
To == gsl::dynamic_extent> To == gsl::dynamic_extent>
@ -151,10 +153,10 @@ namespace details
: span_iterator(other.span_, other.index_) : span_iterator(other.span_, other.index_)
{} {}
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
constexpr reference operator*() const constexpr reference operator*() const
{ {
Expects(index_ != span_->size()); Expects(static_cast<size_t>(index_) != span_->size());
return *(span_->data() + index_); return *(span_->data() + index_);
} }
@ -166,7 +168,7 @@ namespace details
constexpr span_iterator& operator++() noexcept constexpr span_iterator& operator++() noexcept
{ {
Expects(0 <= index_ && index_ != span_->size()); Expects(0 <= index_ && static_cast<size_t>(index_) != span_->size());
++index_; ++index_;
return *this; return *this;
} }
@ -180,7 +182,7 @@ namespace details
constexpr span_iterator& operator--() noexcept constexpr span_iterator& operator--() noexcept
{ {
Expects(index_ != 0 && index_ <= span_->size()); Expects(index_ != 0 && static_cast<size_t>(index_) <= span_->size());
--index_; --index_;
return *this; return *this;
} }
@ -198,7 +200,8 @@ namespace details
return ret += n; return ret += n;
} }
friend constexpr span_iterator operator+(difference_type n, span_iterator const& rhs) noexcept friend constexpr span_iterator operator+(difference_type n,
span_iterator const& rhs) noexcept
{ {
return rhs + n; return rhs + n;
} }
@ -272,7 +275,7 @@ namespace details
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size()); Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
} }
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
constexpr pointer _Unwrapped() const noexcept constexpr pointer _Unwrapped() const noexcept
{ // after seeking *this to a high water mark, or using one of the { // after seeking *this to a high water mark, or using one of the
// _Verify_xxx functions above, unwrap this span_iterator to a raw // _Verify_xxx functions above, unwrap this span_iterator to a raw
@ -287,7 +290,7 @@ namespace details
#else #else
static constexpr bool _Unwrap_when_unverified = false; static constexpr bool _Unwrap_when_unverified = false;
#endif #endif
GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive GSL_SUPPRESS(con .3) // NO-FORMAT: attribute // TODO: false positive
constexpr void _Seek_to(const pointer p) noexcept constexpr void _Seek_to(const pointer p) noexcept
{ // adjust the position of *this to previously verified location p { // adjust the position of *this to previously verified location p
// after _Unwrapped // after _Unwrapped
@ -300,11 +303,11 @@ namespace details
std::ptrdiff_t index_ = 0; std::ptrdiff_t index_ = 0;
}; };
template <std::ptrdiff_t Ext> template <std::size_t Ext>
class extent_type class extent_type
{ {
public: public:
using index_type = std::ptrdiff_t; using index_type = std::size_t;
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size."); static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
@ -327,13 +330,16 @@ namespace details
class extent_type<dynamic_extent> class extent_type<dynamic_extent>
{ {
public: public:
using index_type = std::ptrdiff_t; using index_type = std::size_t;
template <index_type Other> template <index_type Other>
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
{} {}
explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); } explicit constexpr extent_type(index_type size) : size_(size)
{
Expects(size != dynamic_extent);
}
constexpr index_type size() const noexcept { return size_; } constexpr index_type size() const noexcept { return size_; }
@ -341,7 +347,7 @@ namespace details
index_type size_; index_type size_;
}; };
template <class ElementType, std::ptrdiff_t Extent, std::ptrdiff_t Offset, std::ptrdiff_t Count> template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
struct calculate_subspan_type struct calculate_subspan_type
{ {
using type = span<ElementType, Count != dynamic_extent using type = span<ElementType, Count != dynamic_extent
@ -351,16 +357,17 @@ namespace details
} // namespace details } // namespace details
// [span], class template span // [span], class template span
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
class span class span
{ {
public: public:
// constants and types // constants and types
using element_type = ElementType; using element_type = ElementType;
using value_type = std::remove_cv_t<ElementType>; using value_type = std::remove_cv_t<ElementType>;
using index_type = std::ptrdiff_t; using index_type = std::size_t;
using pointer = element_type*; using pointer = element_type*;
using reference = element_type&; using reference = element_type&;
using difference_type = std::ptrdiff_t;
using iterator = details::span_iterator<span<ElementType, Extent>, false>; using iterator = details::span_iterator<span<ElementType, Extent>, false>;
using const_iterator = details::span_iterator<span<ElementType, Extent>, true>; using const_iterator = details::span_iterator<span<ElementType, Extent>, true>;
@ -379,11 +386,11 @@ public:
template <bool Dependent = false, template <bool Dependent = false,
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE, // "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0. // since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
class = std::enable_if_t<(Dependent || Extent <= 0)>> class = std::enable_if_t<(Dependent || Extent == 0 || Extent == dynamic_extent)>>
constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
{} {}
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, std::distance(firstElem, lastElem))
@ -397,24 +404,20 @@ public:
template <std::size_t N, class = std::enable_if_t<(N > 0)>> template <std::size_t N, class = std::enable_if_t<(N > 0)>>
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>()) : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
{ {}
}
constexpr span(std::array<std::remove_const_t<element_type>, 0>&) noexcept constexpr span(std::array<std::remove_const_t<element_type>, 0>&) noexcept
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>()) : storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
{ {}
}
template <std::size_t N, class = std::enable_if_t<(N > 0)>> template <std::size_t N, class = std::enable_if_t<(N > 0)>>
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>()) : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
{ {}
}
constexpr span(const std::array<std::remove_const_t<element_type>, 0>&) noexcept constexpr span(const std::array<std::remove_const_t<element_type>, 0>&) noexcept
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>()) : storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
{ {}
}
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
// on Container to be a contiguous sequence container. // on Container to be a contiguous sequence container.
@ -433,13 +436,14 @@ public:
std::is_convertible<typename Container::pointer, pointer>::value && std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer, std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>> decltype(std::declval<Container>().data())>::value>>
constexpr span(const Container& cont) noexcept : span(cont.data(), narrow<index_type>(cont.size())) constexpr span(const Container& cont) noexcept
: span(cont.data(), narrow<index_type>(cont.size()))
{} {}
constexpr span(const span& other) noexcept = default; constexpr span(const span& other) noexcept = default;
template < template <
class OtherElementType, std::ptrdiff_t OtherExtent, class OtherElementType, std::size_t OtherExtent,
class = std::enable_if_t< class = std::enable_if_t<
details::is_allowed_extent_conversion<OtherExtent, Extent>::value && details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>> details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
@ -451,27 +455,27 @@ public:
constexpr span& operator=(const span& other) noexcept = default; constexpr span& operator=(const span& other) noexcept = default;
// [span.sub], span subviews // [span.sub], span subviews
template <std::ptrdiff_t Count> template <std::size_t Count>
constexpr span<element_type, Count> first() const noexcept constexpr span<element_type, Count> first() const noexcept
{ {
Expects(Count >= 0 && Count <= size()); Expects(Count <= size());
return {data(), Count}; return {data(), Count};
} }
template <std::ptrdiff_t Count> template <std::size_t Count>
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
constexpr span<element_type, Count> last() const noexcept constexpr span<element_type, Count> last() const noexcept
{ {
Expects(Count >= 0 && size() - Count >= 0); Expects(size() >= Count);
return {data() + (size() - Count), Count}; return {data() + (size() - Count), Count};
} }
template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent> template <std::size_t Offset, std::size_t Count = dynamic_extent>
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
constexpr auto subspan() const noexcept -> constexpr auto subspan() const noexcept ->
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
{ {
Expects((Offset >= 0 && size() - Offset >= 0) && Expects((size() >= Offset) &&
(Count == dynamic_extent || (Count >= 0 && Offset + Count <= size()))); (Count == dynamic_extent || (Count >= 0 && Offset + Count <= size())));
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count}; return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
@ -479,17 +483,19 @@ public:
constexpr span<element_type, dynamic_extent> first(index_type count) const noexcept constexpr span<element_type, dynamic_extent> first(index_type count) const noexcept
{ {
Expects(count >= 0 && count <= size()); Expects(count <= size());
return {data(), count}; return {data(), count};
} }
constexpr span<element_type, dynamic_extent> last(index_type count) const noexcept constexpr span<element_type, dynamic_extent> last(index_type count) const noexcept
{ {
Expects(count <= size());
return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{}); return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
} }
constexpr span<element_type, dynamic_extent> subspan(index_type offset, constexpr span<element_type, dynamic_extent> subspan(index_type offset,
index_type count = dynamic_extent) const noexcept index_type count = dynamic_extent) const
noexcept
{ {
return make_subspan(offset, count, subspan_selector<Extent>{}); return make_subspan(offset, count, subspan_selector<Extent>{});
} }
@ -503,7 +509,7 @@ public:
constexpr bool empty() const noexcept { return size() == 0; } constexpr bool empty() const noexcept { return size() == 0; }
// [span.elem], span element access // [span.elem], span element access
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(CheckRange(idx, storage_.size()));
@ -512,23 +518,25 @@ public:
constexpr reference front() const noexcept constexpr reference front() const noexcept
{ {
Expects(size() > 0); Expects(size() != dynamic_extent && size() > 0);
return data()[0]; return data()[0];
} }
constexpr reference back() const noexcept constexpr reference back() const noexcept
{ {
Expects(size() > 0); Expects(size() != dynamic_extent && size() > 0);
return data()[size() - 1]; return data()[size() - 1];
} }
// at and operator() are deprecated to align to the public member functions of std::span // at and operator() are deprecated to align to the public member functions of std::span
[[deprecated("Use operator[]")]] [[deprecated("Use operator[]")]] constexpr reference at(index_type idx) const noexcept
constexpr reference at(index_type idx) const noexcept{ return this->operator[](idx); } {
[[deprecated("Use operator[]")]] return this->operator[](idx);
constexpr reference operator()(index_type idx) const noexcept{ return this->operator[](idx); } }
[[deprecated("Use operator[]")]] constexpr reference operator()(index_type idx) const noexcept
{
return this->operator[](idx);
}
constexpr pointer data() const noexcept { return storage_.data(); } constexpr pointer data() const noexcept { return storage_.data(); }
@ -556,7 +564,7 @@ public:
constexpr pointer _Unchecked_begin() const noexcept { return data(); } constexpr pointer _Unchecked_begin() const noexcept { return data(); }
constexpr pointer _Unchecked_end() const noexcept constexpr pointer _Unchecked_end() const noexcept
{ {
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
return data() + size(); return data() + size();
} }
#endif // _MSC_VER #endif // _MSC_VER
@ -607,13 +615,13 @@ private:
constexpr storage_type(KnownNotNull data, OtherExtentType ext) constexpr storage_type(KnownNotNull data, OtherExtentType ext)
: ExtentType(ext), data_(data.p) : ExtentType(ext), data_(data.p)
{ {
Expects(ExtentType::size() >= 0); Expects(ExtentType::size() != dynamic_extent);
} }
template <class OtherExtentType> template <class OtherExtentType>
constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data) constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
{ {
Expects(ExtentType::size() >= 0); Expects(ExtentType::size() != dynamic_extent);
Expects(data || ExtentType::size() == 0); Expects(data || ExtentType::size() == 0);
} }
@ -629,12 +637,12 @@ private:
// in subspans and constructors from arrays // in subspans and constructors from arrays
constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {} constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {}
template <std::ptrdiff_t CallerExtent> template <std::size_t CallerExtent>
class subspan_selector class subspan_selector
{ {
}; };
template <std::ptrdiff_t CallerExtent> template <std::size_t CallerExtent>
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count, span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
subspan_selector<CallerExtent>) const subspan_selector<CallerExtent>) const
{ {
@ -642,56 +650,56 @@ private:
return tmp.subspan(offset, count); return tmp.subspan(offset, count);
} }
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count, span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
subspan_selector<dynamic_extent>) const subspan_selector<dynamic_extent>) const
{ {
Expects(offset >= 0 && size() - offset >= 0); Expects(offset >= 0 && size() >= offset && size() != dynamic_extent);
if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; } if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
Expects(count >= 0 && size() - offset >= count); Expects(size() - offset >= count);
return {KnownNotNull{data() + offset}, count}; return {KnownNotNull{data() + offset}, count};
} }
}; };
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent; constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent;
#endif #endif
// [span.comparison], span comparison operators // [span.comparison], span comparison operators
template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent> template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r) constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
{ {
return std::equal(l.begin(), l.end(), r.begin(), r.end()); return std::equal(l.begin(), l.end(), r.begin(), r.end());
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r) constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{ {
return !(l == r); return !(l == r);
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r) constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
{ {
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r) constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{ {
return !(l > r); return !(l > r);
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r) constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
{ {
return r < l; return r < l;
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r) constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{ {
return !(l < r); return !(l < r);
@ -705,36 +713,33 @@ namespace details
// we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
// constexpr // constexpr
// and so will fail compilation of the template // and so will fail compilation of the template
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
struct calculate_byte_size struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
: std::integral_constant<std::ptrdiff_t,
static_cast<std::ptrdiff_t>(sizeof(ElementType) *
static_cast<std::size_t>(Extent))>
{ {
}; };
template <class ElementType> template <class ElementType>
struct calculate_byte_size<ElementType, dynamic_extent> struct calculate_byte_size<ElementType, dynamic_extent>
: std::integral_constant<std::ptrdiff_t, dynamic_extent> : std::integral_constant<std::size_t, dynamic_extent>
{ {
}; };
} // namespace details } // namespace details
// [span.objectrep], views of object representation // [span.objectrep], views of object representation
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
span<const byte, details::calculate_byte_size<ElementType, Extent>::value> span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept as_bytes(span<ElementType, Extent> s) noexcept
{ {
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type .1) // NO-FORMAT: attribute
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
} }
template <class ElementType, std::ptrdiff_t Extent, template <class ElementType, std::size_t Extent,
class = std::enable_if_t<!std::is_const<ElementType>::value>> class = std::enable_if_t<!std::is_const<ElementType>::value>>
span<byte, details::calculate_byte_size<ElementType, Extent>::value> span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(span<ElementType, Extent> s) noexcept as_writeable_bytes(span<ElementType, Extent> s) noexcept
{ {
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type .1) // NO-FORMAT: attribute
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
} }
@ -773,7 +778,7 @@ constexpr span<const typename Container::value_type> make_span(const Container&
} }
template <class Ptr> template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::ptrdiff_t count) constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
{ {
return span<typename Ptr::element_type>(cont, count); return span<typename Ptr::element_type>(cont, count);
} }
@ -785,7 +790,7 @@ constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
} }
// Specialization of gsl::at for span // Specialization of gsl::at for span
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, index i) constexpr ElementType& at(span<ElementType, Extent> s, index i)
{ {
// No bounds checking here because it is done in span::operator[] called below // No bounds checking here because it is done in span::operator[] called below
@ -793,57 +798,66 @@ constexpr ElementType& at(span<ElementType, Extent> s, index i)
} }
// [span.obs] Free observer functions // [span.obs] Free observer functions
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::index_type ssize(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::index_type
ssize(const span<ElementType, Extent>& span) noexcept
{ {
return span.size(); return span.size();
} }
// [span.iter] Free functions for begin/end functions // [span.iter] Free functions for begin/end functions
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::iterator begin(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::iterator
begin(const span<ElementType, Extent>& span) noexcept
{ {
return span.begin(); return span.begin();
} }
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> template <class ElementType, std::size_t Extent = dynamic_extent>
constexpr typename span<ElementType, Extent>::iterator end(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::iterator
end(const span<ElementType, Extent>& span) noexcept
{ {
return span.end(); return span.end();
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_iterator cbegin(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::const_iterator
cbegin(const span<ElementType, Extent>& span) noexcept
{ {
return span.cbegin(); return span.cbegin();
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_iterator cend(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::const_iterator
cend(const span<ElementType, Extent>& span) noexcept
{ {
return span.cend(); return span.cend();
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator rbegin(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::reverse_iterator
rbegin(const span<ElementType, Extent>& span) noexcept
{ {
return span.rbegin(); return span.rbegin();
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator rend(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::reverse_iterator
rend(const span<ElementType, Extent>& span) noexcept
{ {
return span.rend(); return span.rend();
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_reverse_iterator crbegin(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::const_reverse_iterator
crbegin(const span<ElementType, Extent>& span) noexcept
{ {
return span.crbegin(); return span.crbegin();
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_reverse_iterator crend(const span<ElementType, Extent> &span) noexcept constexpr typename span<ElementType, Extent>::const_reverse_iterator
crend(const span<ElementType, Extent>& span) noexcept
{ {
return span.crend(); return span.crend();
} }

View File

@ -23,7 +23,7 @@
#include <algorithm> // for equal, lexicographical_compare #include <algorithm> // for equal, lexicographical_compare
#include <array> // for array #include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t #include <cstddef> // for size_t, nullptr_t
#include <cstdint> // for PTRDIFF_MAX #include <cstdint> // for PTRDIFF_MAX
#include <cstring> #include <cstring>
#include <string> // for basic_string, allocator, char_traits #include <string> // for basic_string, allocator, char_traits
@ -56,43 +56,43 @@ namespace gsl
// (sometimes needlessly) break existing programs when introduced. // (sometimes needlessly) break existing programs when introduced.
// //
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent> template <typename CharT, std::size_t Extent = dynamic_extent>
using basic_zstring = CharT*; using basic_zstring = CharT*;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using czstring = basic_zstring<const char, Extent>; using czstring = basic_zstring<const char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cwzstring = basic_zstring<const wchar_t, Extent>; using cwzstring = basic_zstring<const wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cu16zstring = basic_zstring<const char16_t, Extent>; using cu16zstring = basic_zstring<const char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cu32zstring = basic_zstring<const char32_t, Extent>; using cu32zstring = basic_zstring<const char32_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using zstring = basic_zstring<char, Extent>; using zstring = basic_zstring<char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using wzstring = basic_zstring<wchar_t, Extent>; using wzstring = basic_zstring<wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using u16zstring = basic_zstring<char16_t, Extent>; using u16zstring = basic_zstring<char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using u32zstring = basic_zstring<char32_t, Extent>; using u32zstring = basic_zstring<char32_t, Extent>;
namespace details namespace details
{ {
template <class CharT> template <class CharT>
std::ptrdiff_t string_length(const CharT* str, std::ptrdiff_t n) std::size_t string_length(const CharT* str, std::size_t n)
{ {
if (str == nullptr || n <= 0) return 0; if (str == nullptr || n == dynamic_extent) return 0;
const span<const CharT> str_span{str, n}; const span<const CharT> str_span{str, n};
std::ptrdiff_t len = 0; std::size_t len = 0;
while (len < n && str_span[len]) len++; while (len < n && str_span[len]) len++;
return len; return len;
@ -108,18 +108,20 @@ namespace details
// Will fail-fast if sentinel cannot be found before max elements are examined. // Will fail-fast if sentinel cannot be found before max elements are examined.
// //
template <typename T, const T Sentinel> template <typename T, const T Sentinel>
span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX) span<T, dynamic_extent> ensure_sentinel(T* seq,
std::size_t max = std::numeric_limits<std::size_t>::max())
{ {
Ensures(seq != nullptr); Ensures(seq != nullptr);
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work GSL_SUPPRESS(
f .23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
auto cur = seq; auto cur = seq;
Ensures(cur != nullptr); // workaround for removing the warning Ensures(cur != nullptr); // workaround for removing the warning
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute // TODO: suppress does not work GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute // TODO: suppress does not work
while ((cur - seq) < max && *cur != Sentinel) ++cur; while (static_cast<std::size_t>(cur - seq) < max && *cur != Sentinel) ++cur;
Ensures(*cur == Sentinel); Ensures(*cur == Sentinel);
return {seq, cur - seq}; return {seq, static_cast<std::size_t>(cur - seq)};
} }
// //
@ -128,7 +130,8 @@ span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX
// the limit of size_type. // the limit of size_type.
// //
template <typename CharT> template <typename CharT>
span<CharT, dynamic_extent> ensure_z(CharT* const& sz, std::ptrdiff_t max = PTRDIFF_MAX) span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
std::size_t max = std::numeric_limits<std::size_t>::max())
{ {
return ensure_sentinel<CharT, CharT(0)>(sz, max); return ensure_sentinel<CharT, CharT(0)>(sz, max);
} }
@ -136,26 +139,27 @@ span<CharT, dynamic_extent> ensure_z(CharT* const& sz, std::ptrdiff_t max = PTRD
template <typename CharT, std::size_t N> template <typename CharT, std::size_t N>
span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N]) span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
{ {
return ensure_z(&sz[0], narrow_cast<std::ptrdiff_t>(N)); return ensure_z(&sz[0], N);
} }
template <class Cont> template <class Cont>
span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent> span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
ensure_z(Cont& cont) ensure_z(Cont& cont)
{ {
return ensure_z(cont.data(), narrow_cast<std::ptrdiff_t>(cont.size())); return ensure_z(cont.data(), cont.size());
} }
template <typename CharT, std::ptrdiff_t> template <typename CharT, std::size_t>
class basic_string_span; class basic_string_span;
namespace details { namespace details
{
template <typename T> template <typename T>
struct is_basic_string_span_oracle : std::false_type struct is_basic_string_span_oracle : std::false_type
{ {
}; };
template <typename CharT, std::ptrdiff_t Extent> template <typename CharT, std::size_t Extent>
struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
{ {
}; };
@ -169,7 +173,7 @@ namespace details {
// //
// string_span and relatives // string_span and relatives
// //
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent> template <typename CharT, std::size_t Extent = dynamic_extent>
class basic_string_span class basic_string_span
{ {
public: public:
@ -218,7 +222,7 @@ public:
template <class Traits, class Allocator> template <class Traits, class Allocator>
// GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug // GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug
constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str) constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
: span_(&str[0], narrow_cast<std::ptrdiff_t>(str.length())) : span_(&str[0], str.length())
{} {}
template <class Traits, class Allocator> template <class Traits, class Allocator>
@ -247,7 +251,7 @@ public:
// from string_span // from string_span
template < template <
class OtherValueType, std::ptrdiff_t OtherExtent, class OtherValueType, std::size_t OtherExtent,
class = std::enable_if_t<std::is_convertible< class = std::enable_if_t<std::is_convertible<
typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>> typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other) constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
@ -312,7 +316,7 @@ public:
constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); } constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); }
private: private:
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max) static impl_type remove_z(pointer const& sz, std::size_t max)
{ {
return {sz, details::string_length(sz, max)}; return {sz, details::string_length(sz, max)};
} }
@ -320,41 +324,41 @@ private:
template <std::size_t N> template <std::size_t N>
static impl_type remove_z(element_type (&sz)[N]) static impl_type remove_z(element_type (&sz)[N])
{ {
return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N)); return remove_z(&sz[0], N);
} }
impl_type span_; impl_type span_;
}; };
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using string_span = basic_string_span<char, Extent>; using string_span = basic_string_span<char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cstring_span = basic_string_span<const char, Extent>; using cstring_span = basic_string_span<const char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using wstring_span = basic_string_span<wchar_t, Extent>; using wstring_span = basic_string_span<wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cwstring_span = basic_string_span<const wchar_t, Extent>; using cwstring_span = basic_string_span<const wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using u16string_span = basic_string_span<char16_t, Extent>; using u16string_span = basic_string_span<char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cu16string_span = basic_string_span<const char16_t, Extent>; using cu16string_span = basic_string_span<const char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using u32string_span = basic_string_span<char32_t, Extent>; using u32string_span = basic_string_span<char32_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent> template <std::size_t Extent = dynamic_extent>
using cu32string_span = basic_string_span<const char32_t, Extent>; using cu32string_span = basic_string_span<const char32_t, Extent>;
// //
// to_string() allow (explicit) conversions from string_span to string // to_string() allow (explicit) conversions from string_span to string
// //
template <typename CharT, std::ptrdiff_t Extent> template <typename CharT, std::size_t Extent>
std::basic_string<typename std::remove_const<CharT>::type> std::basic_string<typename std::remove_const<CharT>::type>
to_string(basic_string_span<CharT, Extent> view) to_string(basic_string_span<CharT, Extent> view)
{ {
@ -362,33 +366,34 @@ to_string(basic_string_span<CharT, Extent> view)
} }
template <typename CharT, typename Traits = typename std::char_traits<CharT>, template <typename CharT, typename Traits = typename std::char_traits<CharT>,
typename Allocator = std::allocator<CharT>, typename gCharT, std::ptrdiff_t Extent> typename Allocator = std::allocator<CharT>, typename gCharT, std::size_t Extent>
std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view) std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
{ {
return {view.data(), narrow_cast<std::size_t>(view.length())}; return {view.data(), narrow_cast<std::size_t>(view.length())};
} }
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::size_t Extent>
basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value> basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(basic_string_span<ElementType, Extent> s) noexcept as_bytes(basic_string_span<ElementType, Extent> s) noexcept
{ {
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type .1) // NO-FORMAT: attribute
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
} }
template <class ElementType, std::ptrdiff_t Extent, template <class ElementType, std::size_t Extent,
class = std::enable_if_t<!std::is_const<ElementType>::value>> class = std::enable_if_t<!std::is_const<ElementType>::value>>
basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value> basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(basic_string_span<ElementType, Extent> s) noexcept as_writeable_bytes(basic_string_span<ElementType, Extent> s) noexcept
{ {
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type .1) // NO-FORMAT: attribute
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
} }
// zero-terminated string span, used to convert // zero-terminated string span, used to convert
// zero-terminated spans to legacy strings // zero-terminated spans to legacy strings
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent> template <typename CharT, std::size_t Extent = dynamic_extent>
class basic_zstring_span { class basic_zstring_span
{
public: public:
using value_type = CharT; using value_type = CharT;
using const_value_type = std::add_const_t<CharT>; using const_value_type = std::add_const_t<CharT>;
@ -435,32 +440,32 @@ private:
impl_type span_; impl_type span_;
}; };
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using zstring_span = basic_zstring_span<char, Max>; using zstring_span = basic_zstring_span<char, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using wzstring_span = basic_zstring_span<wchar_t, Max>; using wzstring_span = basic_zstring_span<wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using u16zstring_span = basic_zstring_span<char16_t, Max>; using u16zstring_span = basic_zstring_span<char16_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using u32zstring_span = basic_zstring_span<char32_t, Max>; using u32zstring_span = basic_zstring_span<char32_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using czstring_span = basic_zstring_span<const char, Max>; using czstring_span = basic_zstring_span<const char, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using cwzstring_span = basic_zstring_span<const wchar_t, Max>; using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using cu16zstring_span = basic_zstring_span<const char16_t, Max>; using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent> template <std::size_t Max = dynamic_extent>
using cu32zstring_span = basic_zstring_span<const char32_t, Max>; using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
// operator == // operator ==
template <class CharT, std::ptrdiff_t Extent, class T, template <class CharT, std::size_t Extent, class T,
class = std::enable_if_t< class = std::enable_if_t<
details::is_basic_string_span<T>::value || details::is_basic_string_span<T>::value ||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>> std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
@ -470,7 +475,7 @@ bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end()); return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
} }
template <class CharT, std::ptrdiff_t Extent, class T, template <class CharT, std::size_t Extent, class T,
class = std::enable_if_t< class = std::enable_if_t<
!details::is_basic_string_span<T>::value && !details::is_basic_string_span<T>::value &&
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>> std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
@ -481,7 +486,7 @@ bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other
} }
// operator != // operator !=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible< typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>> T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other) bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other)
@ -490,7 +495,7 @@ bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>> !gsl::details::is_basic_string_span<T>::value>>
@ -500,7 +505,7 @@ bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other)
} }
// operator< // operator<
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible< typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>> T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
@ -510,7 +515,7 @@ bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>> !gsl::details::is_basic_string_span<T>::value>>
@ -526,7 +531,7 @@ bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
// so the cases below are already covered by the previous operators // so the cases below are already covered by the previous operators
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -540,7 +545,7 @@ bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -555,7 +560,7 @@ bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
#endif #endif
// operator <= // operator <=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible< typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>> T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
@ -564,7 +569,7 @@ bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>> !gsl::details::is_basic_string_span<T>::value>>
@ -579,7 +584,7 @@ bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
// so the cases below are already covered by the previous operators // so the cases below are already covered by the previous operators
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -592,7 +597,7 @@ bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -606,7 +611,7 @@ bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
#endif #endif
// operator> // operator>
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible< typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>> T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
@ -615,7 +620,7 @@ bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>> !gsl::details::is_basic_string_span<T>::value>>
@ -630,7 +635,7 @@ bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
// so the cases below are already covered by the previous operators // so the cases below are already covered by the previous operators
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -643,7 +648,7 @@ bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -657,7 +662,7 @@ bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
#endif #endif
// operator >= // operator >=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible< typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>> T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
@ -666,7 +671,7 @@ bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>> !gsl::details::is_basic_string_span<T>::value>>
@ -681,7 +686,7 @@ bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
// so the cases below are already covered by the previous operators // so the cases below are already covered by the previous operators
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
@ -694,7 +699,7 @@ bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
} }
template < template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type, typename DataType = typename T::value_type,
typename = std::enable_if_t< typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&

View File

@ -158,14 +158,14 @@ TEST(span_test, from_pointer_length_constructor)
int arr[4] = {1, 2, 3, 4}; int arr[4] = {1, 2, 3, 4};
{ {
for (int i = 0; i < 4; ++i) for (std::size_t i = 0; i < 4; ++i)
{ {
{ {
span<int> s = {&arr[0], i}; span<int> s = {&arr[0], i};
EXPECT_TRUE(s.size() == i); EXPECT_TRUE(s.size() == i);
EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s.data() == &arr[0]);
EXPECT_TRUE(s.empty() == (i == 0)); EXPECT_TRUE(s.empty() == (i == 0));
for (int j = 0; j < i; ++j) for (std::size_t j = 0; j < i; ++j)
{ {
EXPECT_TRUE(arr[j] == s[j]); EXPECT_TRUE(arr[j] == s[j]);
EXPECT_TRUE(arr[j] == s.at(j)); EXPECT_TRUE(arr[j] == s.at(j));
@ -173,12 +173,12 @@ TEST(span_test, from_pointer_length_constructor)
} }
} }
{ {
span<int> s = {&arr[i], 4 - narrow_cast<ptrdiff_t>(i)}; span<int> s = {&arr[i], 4 - i};
EXPECT_TRUE(s.size() == 4 - i); EXPECT_TRUE(s.size() == 4 - i);
EXPECT_TRUE(s.data() == &arr[i]); EXPECT_TRUE(s.data() == &arr[i]);
EXPECT_TRUE(s.empty() == ((4 - i) == 0)); EXPECT_TRUE(s.empty() == ((4 - i) == 0));
for (int j = 0; j < 4 - i; ++j) for (std::size_t j = 0; j < 4 - i; ++j)
{ {
EXPECT_TRUE(arr[j + i] == s[j]); EXPECT_TRUE(arr[j + i] == s[j]);
EXPECT_TRUE(arr[j + i] == s.at(j)); EXPECT_TRUE(arr[j + i] == s.at(j));
@ -457,21 +457,21 @@ TEST(span_test, from_array_constructor)
{ {
span<int> s{arr}; span<int> s{arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
span<const int> cs{arr}; span<const int> cs{arr};
EXPECT_TRUE(cs.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(cs.size() == arr.size());
EXPECT_TRUE(cs.data() == arr.data()); EXPECT_TRUE(cs.data() == arr.data());
} }
{ {
span<int, 4> s{arr}; span<int, 4> s{arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
span<const int, 4> cs{arr}; span<const int, 4> cs{arr};
EXPECT_TRUE(cs.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(cs.size() == arr.size());
EXPECT_TRUE(cs.data() == arr.data()); EXPECT_TRUE(cs.data() == arr.data());
} }
@ -486,7 +486,7 @@ TEST(span_test, from_array_constructor)
{ {
span<AddressOverloaded, 4> fs{ao_arr}; span<AddressOverloaded, 4> fs{ao_arr};
EXPECT_TRUE(fs.size() == narrow_cast<ptrdiff_t>(ao_arr.size())); EXPECT_TRUE(fs.size() == ao_arr.size());
EXPECT_TRUE(ao_arr.data() == fs.data()); EXPECT_TRUE(ao_arr.data() == fs.data());
} }
@ -532,7 +532,7 @@ TEST(span_test, from_array_constructor)
{ {
auto s = make_span(arr); auto s = make_span(arr);
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
@ -561,13 +561,13 @@ TEST(span_test, from_array_constructor)
{ {
span<const int> s{arr}; span<const int> s{arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
{ {
span<const int, 4> s{arr}; span<const int, 4> s{arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
@ -575,7 +575,7 @@ TEST(span_test, from_array_constructor)
{ {
span<const AddressOverloaded, 4> s{ao_arr}; span<const AddressOverloaded, 4> s{ao_arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(ao_arr.size())); EXPECT_TRUE(s.size() == ao_arr.size());
EXPECT_TRUE(s.data() == ao_arr.data()); EXPECT_TRUE(s.data() == ao_arr.data());
} }
@ -606,7 +606,7 @@ TEST(span_test, from_array_constructor)
{ {
auto s = make_span(arr); auto s = make_span(arr);
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
} }
@ -617,13 +617,13 @@ TEST(span_test, from_array_constructor)
{ {
span<const int> s{arr}; span<const int> s{arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
{ {
span<const int, 4> s{arr}; span<const int, 4> s{arr};
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
@ -651,7 +651,7 @@ TEST(span_test, from_array_constructor)
{ {
auto s = make_span(arr); auto s = make_span(arr);
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size())); EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
} }
@ -663,11 +663,11 @@ TEST(span_test, from_array_constructor)
{ {
span<int> s{v}; span<int> s{v};
EXPECT_TRUE(s.size() == narrow_cast<std::ptrdiff_t>(v.size())); EXPECT_TRUE(s.size() == v.size());
EXPECT_TRUE(s.data() == v.data()); EXPECT_TRUE(s.data() == v.data());
span<const int> cs{v}; span<const int> cs{v};
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(v.size())); EXPECT_TRUE(cs.size() == v.size());
EXPECT_TRUE(cs.data() == v.data()); EXPECT_TRUE(cs.data() == v.data());
} }
@ -677,11 +677,11 @@ TEST(span_test, from_array_constructor)
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
span<char> s{str}; span<char> s{str};
EXPECT_TRUE(s.size() == narrow_cast<std::ptrdiff_t>(str.size())); EXPECT_TRUE(s.size() == str.size());
EXPECT_TRUE(s.data() == str.data())); EXPECT_TRUE(s.data() == str.data()));
#endif #endif
span<const char> cs{str}; span<const char> cs{str};
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(str.size())); EXPECT_TRUE(cs.size() == str.size());
EXPECT_TRUE(cs.data() == str.data()); EXPECT_TRUE(cs.data() == str.data());
} }
@ -690,7 +690,7 @@ TEST(span_test, from_array_constructor)
span<char> s{cstr}; span<char> s{cstr};
#endif #endif
span<const char> cs{cstr}; span<const char> cs{cstr};
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(cstr.size())); EXPECT_TRUE(cs.size() == cstr.size());
EXPECT_TRUE(cs.data() == cstr.data()); EXPECT_TRUE(cs.data() == cstr.data());
} }
@ -745,11 +745,11 @@ TEST(span_test, from_array_constructor)
{ {
auto s = make_span(v); auto s = make_span(v);
EXPECT_TRUE(s.size() == narrow_cast<std::ptrdiff_t>(v.size())); EXPECT_TRUE(s.size() == v.size());
EXPECT_TRUE(s.data() == v.data()); EXPECT_TRUE(s.data() == v.data());
auto cs = make_span(cv); auto cs = make_span(cv);
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(cv.size())); EXPECT_TRUE(cs.size() == cv.size());
EXPECT_TRUE(cs.data() == cv.data()); EXPECT_TRUE(cs.data() == cv.data());
} }
} }