First part of P1227R2 - change span over to use 'size_t' instead of 'ptrdiff_t'. Reviewed as https://reviews.llvm.org/D58639.

llvm-svn: 354936
This commit is contained in:
Marshall Clow 2019-02-27 00:32:16 +00:00
parent 129826cd9f
commit 7ad06a9319
7 changed files with 47 additions and 51 deletions

View File

@ -1442,7 +1442,7 @@ private:
template <class _Up> friend class __wrap_iter;
template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
template <class _Tp, ptrdiff_t> friend class _LIBCPP_TEMPLATE_VIS span;
template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
template <class _Iter1, class _Iter2>
_LIBCPP_CONSTEXPR_IF_NODEBUG friend

View File

@ -16,36 +16,36 @@
namespace std {
// constants
inline constexpr ptrdiff_t dynamic_extent = -1;
inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
// [views.span], class template span
template <class ElementType, ptrdiff_t Extent = dynamic_extent>
template <class ElementType, size_t Extent = dynamic_extent>
class span;
// [span.objectrep], views of object representation
template <class ElementType, ptrdiff_t Extent>
template <class ElementType, size_t Extent>
span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
(static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
template <class ElementType, ptrdiff_t Extent>
template <class ElementType, size_t Extent>
span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
(static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
namespace std {
template <class ElementType, ptrdiff_t Extent = dynamic_extent>
template <class ElementType, size_t Extent = dynamic_extent>
class span {
public:
// constants and types
using element_type = ElementType;
using value_type = remove_cv_t<ElementType>;
using index_type = ptrdiff_t;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = element_type*;
using const_pointer = const element_type*;
using reference = element_type&;
using iterator = implementation-defined;
using const_reference = const element_type&;
using iterator = implementation-defined;
using const_iterator = implementation-defined;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
@ -66,17 +66,17 @@ public:
template <class Container>
constexpr span(const Container& cont);
constexpr span(const span& other) noexcept = default;
template <class OtherElementType, ptrdiff_t OtherExtent>
template <class OtherElementType, size_t OtherExtent>
constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
// [span.sub], span subviews
template <ptrdiff_t Count>
template <size_t Count>
constexpr span<element_type, Count> first() const;
template <ptrdiff_t Count>
template <size_t Count>
constexpr span<element_type, Count> last() const;
template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
template <size_t Offset, size_t Count = dynamic_extent>
constexpr span<element_type, see below> subspan() const;
constexpr span<element_type, dynamic_extent> first(index_type count) const;
@ -143,14 +143,14 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER > 17
inline constexpr ptrdiff_t dynamic_extent = -1;
template <typename _Tp, ptrdiff_t _Extent = dynamic_extent> class span;
inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
template <typename _Tp, size_t _Extent = dynamic_extent> class span;
template <class _Tp>
struct __is_span_impl : public false_type {};
template <class _Tp, ptrdiff_t _Extent>
template <class _Tp, size_t _Extent>
struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
template <class _Tp>
@ -189,13 +189,13 @@ struct __is_span_compatible_container<_Tp, _ElementType,
: public true_type {};
template <typename _Tp, ptrdiff_t _Extent>
template <typename _Tp, size_t _Extent>
class _LIBCPP_TEMPLATE_VIS span {
public:
// constants and types
using element_type = _Tp;
using value_type = remove_cv_t<_Tp>;
using index_type = ptrdiff_t;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = _Tp *;
using const_pointer = const _Tp *;
@ -244,20 +244,18 @@ public:
// ~span() noexcept = default;
template <ptrdiff_t _Count>
template <size_t _Count>
inline _LIBCPP_INLINE_VISIBILITY
constexpr span<element_type, _Count> first() const noexcept
{
static_assert(_Count >= 0, "Count must be >= 0 in span::first()");
static_assert(_Count <= _Extent, "Count out of range in span::first()");
return {data(), _Count};
}
template <ptrdiff_t _Count>
template <size_t _Count>
inline _LIBCPP_INLINE_VISIBILITY
constexpr span<element_type, _Count> last() const noexcept
{
static_assert(_Count >= 0, "Count must be >= 0 in span::last()");
static_assert(_Count <= _Extent, "Count out of range in span::last()");
return {data() + size() - _Count, _Count};
}
@ -276,7 +274,7 @@ public:
return {data() + size() - __count, __count};
}
template <ptrdiff_t _Offset, ptrdiff_t _Count = dynamic_extent>
template <size_t _Offset, size_t _Count = dynamic_extent>
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto subspan() const noexcept
-> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
@ -359,7 +357,7 @@ public:
// constants and types
using element_type = _Tp;
using value_type = remove_cv_t<_Tp>;
using index_type = ptrdiff_t;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = _Tp *;
using const_pointer = const _Tp *;
@ -379,7 +377,7 @@ public:
constexpr span& operator=(const span&) noexcept = default;
_LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}, __size{__count} {}
_LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{distance(__f, __l)} {}
_LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{static_cast<size_t>(distance(__f, __l))} {}
template <size_t _Sz>
inline _LIBCPP_INLINE_VISIBILITY
@ -406,7 +404,7 @@ public:
: __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {}
template <class _OtherElementType, ptrdiff_t _OtherExtent>
template <class _OtherElementType, size_t _OtherExtent>
inline _LIBCPP_INLINE_VISIBILITY
constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
enable_if_t<
@ -416,20 +414,18 @@ public:
// ~span() noexcept = default;
template <ptrdiff_t _Count>
template <size_t _Count>
inline _LIBCPP_INLINE_VISIBILITY
constexpr span<element_type, _Count> first() const noexcept
{
static_assert(_Count >= 0, "Count must be >= 0 in span::first()");
_LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
return {data(), _Count};
}
template <ptrdiff_t _Count>
template <size_t _Count>
inline _LIBCPP_INLINE_VISIBILITY
constexpr span<element_type, _Count> last() const noexcept
{
static_assert(_Count >= 0, "Count must be >= 0 in span::last()");
_LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
return {data() + size() - _Count, _Count};
}
@ -448,7 +444,7 @@ public:
return {data() + size() - __count, __count};
}
template <ptrdiff_t _Offset, ptrdiff_t _Count = dynamic_extent>
template <size_t _Offset, size_t _Count = dynamic_extent>
inline _LIBCPP_INLINE_VISIBILITY
constexpr span<_Tp, dynamic_extent> subspan() const noexcept
{
@ -527,17 +523,17 @@ private:
};
// as_bytes & as_writeable_bytes
template <class _Tp, ptrdiff_t _Extent>
template <class _Tp, size_t _Extent>
auto as_bytes(span<_Tp, _Extent> __s) noexcept
-> decltype(__s.__as_bytes())
{ return __s.__as_bytes(); }
template <class _Tp, ptrdiff_t _Extent>
template <class _Tp, size_t _Extent>
auto as_writeable_bytes(span<_Tp, _Extent> __s) noexcept
-> typename enable_if<!is_const_v<_Tp>, decltype(__s.__as_writeable_bytes())>::type
{ return __s.__as_writeable_bytes(); }
template <class _Tp, ptrdiff_t _Extent>
template <class _Tp, size_t _Extent>
constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept
{ __lhs.swap(__rhs); }

View File

@ -24,7 +24,7 @@
int main(int, char**)
{
std::span<int, 2> s; // expected-error-re@span:* {{static_assert failed{{( due to requirement '2[LL]{0,2} == 0')?}} "Can't default construct a statically sized span with size > 0"}}
std::span<int, 2> s; // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Can't default construct a statically sized span with size > 0"}}
// TODO: This is what I want:
// eXpected-error {{no matching constructor for initialization of 'std::span<int, 2>'}}

View File

@ -38,8 +38,8 @@ constexpr bool testConstexprSpan(Span s)
ret = ret && (&*(ce-1) == last);
}
ret = ret && (( e - s.begin()) == s.size());
ret = ret && ((ce - s.cbegin()) == s.size());
ret = ret && (static_cast<size_t>( e - s.begin()) == s.size());
ret = ret && (static_cast<size_t>(ce - s.cbegin()) == s.size());
ret = ret && (e == ce);
return ret;
@ -64,8 +64,8 @@ void testRuntimeSpan(Span s)
assert( &*(ce-1) == last);
}
assert(( e - s.begin()) == s.size());
assert((ce - s.cbegin()) == s.size());
assert(static_cast<size_t>( e - s.begin()) == s.size());
assert(static_cast<size_t>(ce - s.cbegin()) == s.size());
assert(e == ce);
}

View File

@ -35,8 +35,8 @@ constexpr bool testConstexprSpan(Span s)
ret = ret && (ce != s.crbegin());
}
ret = ret && (( e - s.rbegin()) == s.size());
ret = ret && ((ce - s.crbegin()) == s.size());
ret = ret && (static_cast<size_t>( e - s.rbegin()) == s.size());
ret = ret && (static_cast<size_t>(ce - s.crbegin()) == s.size());
ret = ret && (e == ce);
return ret;
@ -58,8 +58,8 @@ void testRuntimeSpan(Span s)
assert(ce != s.crbegin());
}
assert(( e - s.rbegin()) == s.size());
assert((ce - s.crbegin()) == s.size());
assert(static_cast<size_t>( e - s.rbegin()) == s.size());
assert(static_cast<size_t>(ce - s.crbegin()) == s.size());
assert(e == ce);
}

View File

@ -22,7 +22,7 @@
template <typename Span>
constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz)
constexpr bool testConstexprSpan(Span sp, size_t sz)
{
ASSERT_NOEXCEPT(sp.size());
return sp.size() == sz;
@ -30,7 +30,7 @@ constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz)
template <typename Span>
void testRuntimeSpan(Span sp, ptrdiff_t sz)
void testRuntimeSpan(Span sp, size_t sz)
{
ASSERT_NOEXCEPT(sp.size());
assert(sp.size() == sz);

View File

@ -16,7 +16,7 @@
// // constants and types
// using element_type = ElementType;
// using value_type = remove_cv_t<ElementType>;
// using index_type = ptrdiff_t;
// using index_type = size_t;
// using difference_type = ptrdiff_t;
// using pointer = element_type *;
// using reference = element_type &;
@ -63,12 +63,12 @@ void testConstIterator()
ASSERT_SAME_TYPE(typename ItT::difference_type, typename S::difference_type);
}
template <typename S, typename ElementType, std::ptrdiff_t Size>
template <typename S, typename ElementType, std::size_t Size>
void testSpan()
{
ASSERT_SAME_TYPE(typename S::element_type, ElementType);
ASSERT_SAME_TYPE(typename S::value_type, std::remove_cv_t<ElementType>);
ASSERT_SAME_TYPE(typename S::index_type, std::ptrdiff_t);
ASSERT_SAME_TYPE(typename S::index_type, std::size_t);
ASSERT_SAME_TYPE(typename S::difference_type, std::ptrdiff_t);
ASSERT_SAME_TYPE(typename S::pointer, ElementType *);
ASSERT_SAME_TYPE(typename S::const_pointer, const ElementType *);
@ -87,10 +87,10 @@ void testSpan()
template <typename T>
void test()
{
testSpan<std::span< T>, T, -1>();
testSpan<std::span<const T>, const T, -1>();
testSpan<std::span< volatile T>, volatile T, -1>();
testSpan<std::span<const volatile T>, const volatile T, -1>();
testSpan<std::span< T>, T, std::dynamic_extent>();
testSpan<std::span<const T>, const T, std::dynamic_extent>();
testSpan<std::span< volatile T>, volatile T, std::dynamic_extent>();
testSpan<std::span<const volatile T>, const volatile T, std::dynamic_extent>();
testSpan<std::span< T, 5>, T, 5>();
testSpan<std::span<const T, 5>, const T, 5>();