xref: /llvm-project-15.0.7/libcxx/include/span (revision 849e749d)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SPAN
11#define _LIBCPP_SPAN
12
13/*
14    span synopsis
15
16namespace std {
17
18// constants
19inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
20
21// [views.span], class template span
22template <class ElementType, size_t Extent = dynamic_extent>
23    class span;
24
25template<class ElementType, size_t Extent>
26  inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
27
28template<class ElementType, size_t Extent>
29    inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
30
31// [span.objectrep], views of object representation
32template <class ElementType, size_t Extent>
33    span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
34        (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
35
36template <class ElementType, size_t Extent>
37    span<      byte, ((Extent == dynamic_extent) ? dynamic_extent :
38        (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
39
40
41template <class ElementType, size_t Extent = dynamic_extent>
42class span {
43public:
44    // constants and types
45    using element_type = ElementType;
46    using value_type = remove_cv_t<ElementType>;
47    using size_type = size_t;
48    using difference_type = ptrdiff_t;
49    using pointer = element_type*;
50    using const_pointer = const element_type*;
51    using reference = element_type&;
52    using const_reference = const element_type&;
53    using iterator = implementation-defined;
54    using reverse_iterator = std::reverse_iterator<iterator>;
55    static constexpr size_type extent = Extent;
56
57    // [span.cons], span constructors, copy, assignment, and destructor
58    constexpr span() noexcept;
59    template <class It>
60    constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
61    template <class It, class End>
62    constexpr explicit(Extent != dynamic_extent) span(It first, End last);
63    template <size_t N>
64        constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept;
65    template <size_t N>
66        constexpr span(array<value_type, N>& arr) noexcept;
67    template <size_t N>
68        constexpr span(const array<value_type, N>& arr) noexcept;
69    template<class R>
70      constexpr explicit(Extent != dynamic_extent) span(R&& r);
71    constexpr span(const span& other) noexcept = default;
72    template <class OtherElementType, size_t OtherExtent>
73        constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
74    ~span() noexcept = default;
75    constexpr span& operator=(const span& other) noexcept = default;
76
77    // [span.sub], span subviews
78    template <size_t Count>
79        constexpr span<element_type, Count> first() const;
80    template <size_t Count>
81        constexpr span<element_type, Count> last() const;
82    template <size_t Offset, size_t Count = dynamic_extent>
83        constexpr span<element_type, see below> subspan() const;
84
85    constexpr span<element_type, dynamic_extent> first(size_type count) const;
86    constexpr span<element_type, dynamic_extent> last(size_type count) const;
87    constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
88
89    // [span.obs], span observers
90    constexpr size_type size() const noexcept;
91    constexpr size_type size_bytes() const noexcept;
92    [[nodiscard]] constexpr bool empty() const noexcept;
93
94    // [span.elem], span element access
95    constexpr reference operator[](size_type idx) const;
96    constexpr reference front() const;
97    constexpr reference back() const;
98    constexpr pointer data() const noexcept;
99
100    // [span.iterators], span iterator support
101    constexpr iterator begin() const noexcept;
102    constexpr iterator end() const noexcept;
103    constexpr reverse_iterator rbegin() const noexcept;
104    constexpr reverse_iterator rend() const noexcept;
105
106private:
107    pointer data_;    // exposition only
108    size_type size_;  // exposition only
109};
110
111template<class It, class EndOrSize>
112    span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
113
114template<class T, size_t N>
115    span(T (&)[N]) -> span<T, N>;
116
117template<class T, size_t N>
118    span(array<T, N>&) -> span<T, N>;
119
120template<class T, size_t N>
121    span(const array<T, N>&) -> span<const T, N>;
122
123template<class R>
124    span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
125
126} // namespace std
127
128*/
129
130#include <__assert>
131#include <__config>
132#include <__debug>
133#include <__iterator/concepts.h>
134#include <__iterator/wrap_iter.h>
135#include <__ranges/concepts.h>
136#include <__ranges/data.h>
137#include <__ranges/enable_borrowed_range.h>
138#include <__ranges/enable_view.h>
139#include <__ranges/size.h>
140#include <array>        // for array
141#include <cstddef>      // for byte
142#include <iterator>     // for iterators
143#include <limits>
144#include <type_traits>  // for remove_cv, etc
145#include <version>
146
147#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
148#  pragma GCC system_header
149#endif
150
151_LIBCPP_PUSH_MACROS
152#include <__undef_macros>
153
154_LIBCPP_BEGIN_NAMESPACE_STD
155
156#if _LIBCPP_STD_VER > 17
157
158inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
159template <typename _Tp, size_t _Extent = dynamic_extent> class span;
160
161template <class _Tp>
162struct __is_std_array : false_type {};
163
164template <class _Tp, size_t _Sz>
165struct __is_std_array<array<_Tp, _Sz>> : true_type {};
166
167template <class _Tp>
168struct __is_std_span : false_type {};
169
170template <class _Tp, size_t _Sz>
171struct __is_std_span<span<_Tp, _Sz>> : true_type {};
172
173#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
174template <class _Range, class _ElementType>
175concept __span_compatible_range =
176  ranges::contiguous_range<_Range> &&
177  ranges::sized_range<_Range> &&
178  (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
179  !__is_std_span<remove_cvref_t<_Range>>::value  &&
180  !__is_std_array<remove_cvref_t<_Range>>::value &&
181  !is_array_v<remove_cvref_t<_Range>> &&
182  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
183#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
184
185template <typename _Tp, size_t _Extent>
186class _LIBCPP_TEMPLATE_VIS span {
187public:
188//  constants and types
189    using element_type           = _Tp;
190    using value_type             = remove_cv_t<_Tp>;
191    using size_type              = size_t;
192    using difference_type        = ptrdiff_t;
193    using pointer                = _Tp *;
194    using const_pointer          = const _Tp *;
195    using reference              = _Tp &;
196    using const_reference        = const _Tp &;
197#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
198    using iterator               = pointer;
199#else
200    using iterator               = __wrap_iter<pointer>;
201#endif
202    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
203
204    static constexpr size_type extent = _Extent;
205
206// [span.cons], span constructors, copy, assignment, and destructor
207    template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
208    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
209
210    constexpr span           (const span&) noexcept = default;
211    constexpr span& operator=(const span&) noexcept = default;
212
213    template <class _It,
214              enable_if_t<contiguous_iterator<_It> &&
215                              is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
216                          nullptr_t> = nullptr>
217    _LIBCPP_INLINE_VISIBILITY
218    constexpr explicit span(_It __first, size_type __count)
219        : __data{_VSTD::to_address(__first)} {
220      (void)__count;
221      _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
222    }
223
224    template <
225        class _It, class _End,
226        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
227                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
228                    nullptr_t> = nullptr>
229    _LIBCPP_INLINE_VISIBILITY
230    constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
231      (void)__last;
232      _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
233      _LIBCPP_ASSERT(__last - __first == _Extent,
234                     "invalid range in span's constructor (iterator, sentinel): last - first != extent");
235    }
236
237    _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}
238
239    template <class _OtherElementType,
240              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
241    _LIBCPP_INLINE_VISIBILITY
242    constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
243
244    template <class _OtherElementType,
245              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
246    _LIBCPP_INLINE_VISIBILITY
247    constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
248
249#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
250    template <__span_compatible_range<element_type> _Range>
251    _LIBCPP_INLINE_VISIBILITY
252    constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
253      _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
254    }
255#endif
256
257    template <class _OtherElementType>
258    _LIBCPP_INLINE_VISIBILITY
259        constexpr span(const span<_OtherElementType, _Extent>& __other,
260                       enable_if_t<
261                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
262                          nullptr_t> = nullptr)
263        : __data{__other.data()} {}
264
265    template <class _OtherElementType>
266    _LIBCPP_INLINE_VISIBILITY
267        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
268                       enable_if_t<
269                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
270                          nullptr_t> = nullptr) noexcept
271        : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
272
273
274//  ~span() noexcept = default;
275
276    template <size_t _Count>
277    _LIBCPP_INLINE_VISIBILITY
278    constexpr span<element_type, _Count> first() const noexcept
279    {
280        static_assert(_Count <= _Extent, "Count out of range in span::first()");
281        return span<element_type, _Count>{data(), _Count};
282    }
283
284    template <size_t _Count>
285    _LIBCPP_INLINE_VISIBILITY
286    constexpr span<element_type, _Count> last() const noexcept
287    {
288        static_assert(_Count <= _Extent, "Count out of range in span::last()");
289        return span<element_type, _Count>{data() + size() - _Count, _Count};
290    }
291
292    _LIBCPP_INLINE_VISIBILITY
293    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
294    {
295        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
296        return {data(), __count};
297    }
298
299    _LIBCPP_INLINE_VISIBILITY
300    constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
301    {
302        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
303        return {data() + size() - __count, __count};
304    }
305
306    template <size_t _Offset, size_t _Count = dynamic_extent>
307    _LIBCPP_INLINE_VISIBILITY
308    constexpr auto subspan() const noexcept
309        -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
310    {
311        static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
312        static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
313
314        using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
315        return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
316    }
317
318
319    _LIBCPP_INLINE_VISIBILITY
320    constexpr span<element_type, dynamic_extent>
321       subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
322    {
323        _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
324        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
325        if (__count == dynamic_extent)
326            return {data() + __offset, size() - __offset};
327        _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
328        return {data() + __offset, __count};
329    }
330
331    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return _Extent; }
332    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return _Extent * sizeof(element_type); }
333    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
334
335    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
336    {
337        _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
338        return __data[__idx];
339    }
340
341    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
342    {
343        _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
344        return __data[0];
345    }
346
347    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
348    {
349        _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
350        return __data[size()-1];
351    }
352
353    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
354
355// [span.iter], span iterator support
356    _LIBCPP_INLINE_VISIBILITY constexpr iterator                 begin() const noexcept { return iterator(data()); }
357    _LIBCPP_INLINE_VISIBILITY constexpr iterator                   end() const noexcept { return iterator(data() + size()); }
358    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
359    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
360
361    _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
362    { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
363
364    _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
365    { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
366
367private:
368    pointer    __data;
369
370};
371
372
373template <typename _Tp>
374class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
375private:
376
377public:
378//  constants and types
379    using element_type           = _Tp;
380    using value_type             = remove_cv_t<_Tp>;
381    using size_type              = size_t;
382    using difference_type        = ptrdiff_t;
383    using pointer                = _Tp *;
384    using const_pointer          = const _Tp *;
385    using reference              = _Tp &;
386    using const_reference        = const _Tp &;
387#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
388    using iterator               = pointer;
389#else
390    using iterator               = __wrap_iter<pointer>;
391#endif
392    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
393
394    static constexpr size_type extent = dynamic_extent;
395
396// [span.cons], span constructors, copy, assignment, and destructor
397    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
398
399    constexpr span           (const span&) noexcept = default;
400    constexpr span& operator=(const span&) noexcept = default;
401
402    template <class _It,
403              enable_if_t<contiguous_iterator<_It> &&
404                              is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
405                          nullptr_t> = nullptr>
406    _LIBCPP_INLINE_VISIBILITY
407    constexpr span(_It __first, size_type __count)
408        : __data{_VSTD::to_address(__first)}, __size{__count} {}
409
410    template <
411        class _It, class _End,
412        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
413                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
414                    nullptr_t> = nullptr>
415    _LIBCPP_INLINE_VISIBILITY
416    constexpr span(_It __first, _End __last)
417        : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
418
419    template <size_t _Sz>
420    _LIBCPP_INLINE_VISIBILITY
421    constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
422
423    template <class _OtherElementType, size_t _Sz,
424              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
425    _LIBCPP_INLINE_VISIBILITY
426    constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
427
428    template <class _OtherElementType, size_t _Sz,
429              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
430    _LIBCPP_INLINE_VISIBILITY
431    constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
432
433#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
434    template <__span_compatible_range<element_type> _Range>
435    _LIBCPP_INLINE_VISIBILITY
436    constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
437#endif
438
439    template <class _OtherElementType, size_t _OtherExtent>
440    _LIBCPP_INLINE_VISIBILITY
441        constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
442                       enable_if_t<
443                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
444                          nullptr_t> = nullptr) noexcept
445        : __data{__other.data()}, __size{__other.size()} {}
446
447//    ~span() noexcept = default;
448
449    template <size_t _Count>
450    _LIBCPP_INLINE_VISIBILITY
451    constexpr span<element_type, _Count> first() const noexcept
452    {
453        _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
454        return span<element_type, _Count>{data(), _Count};
455    }
456
457    template <size_t _Count>
458    _LIBCPP_INLINE_VISIBILITY
459    constexpr span<element_type, _Count> last() const noexcept
460    {
461        _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
462        return span<element_type, _Count>{data() + size() - _Count, _Count};
463    }
464
465    _LIBCPP_INLINE_VISIBILITY
466    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
467    {
468        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
469        return {data(), __count};
470    }
471
472    _LIBCPP_INLINE_VISIBILITY
473    constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
474    {
475        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
476        return {data() + size() - __count, __count};
477    }
478
479    template <size_t _Offset, size_t _Count = dynamic_extent>
480    _LIBCPP_INLINE_VISIBILITY
481    constexpr span<element_type, _Count> subspan() const noexcept
482    {
483        _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
484        _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
485        return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
486    }
487
488    constexpr span<element_type, dynamic_extent>
489    _LIBCPP_INLINE_VISIBILITY
490    subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
491    {
492        _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
493        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
494        if (__count == dynamic_extent)
495            return {data() + __offset, size() - __offset};
496        _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
497        return {data() + __offset, __count};
498    }
499
500    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return __size; }
501    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return __size * sizeof(element_type); }
502    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
503
504    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
505    {
506        _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
507        return __data[__idx];
508    }
509
510    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
511    {
512        _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
513        return __data[0];
514    }
515
516    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
517    {
518        _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
519        return __data[size()-1];
520    }
521
522
523    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
524
525// [span.iter], span iterator support
526    _LIBCPP_INLINE_VISIBILITY constexpr iterator                 begin() const noexcept { return iterator(data()); }
527    _LIBCPP_INLINE_VISIBILITY constexpr iterator                   end() const noexcept { return iterator(data() + size()); }
528    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
529    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
530
531    _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
532    { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
533
534    _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
535    { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
536
537private:
538    pointer   __data;
539    size_type __size;
540};
541
542template <class _Tp, size_t _Extent>
543inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
544
545template <class _ElementType, size_t _Extent>
546inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
547
548//  as_bytes & as_writable_bytes
549template <class _Tp, size_t _Extent>
550_LIBCPP_INLINE_VISIBILITY
551auto as_bytes(span<_Tp, _Extent> __s) noexcept
552-> decltype(__s.__as_bytes())
553{ return    __s.__as_bytes(); }
554
555template <class _Tp, size_t _Extent>
556_LIBCPP_INLINE_VISIBILITY
557auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
558-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
559{ return __s.__as_writable_bytes(); }
560
561#if _LIBCPP_STD_VER > 17
562template<contiguous_iterator _It, class _EndOrSize>
563    span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
564#endif // _LIBCPP_STD_VER > 17
565
566template<class _Tp, size_t _Sz>
567    span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
568
569template<class _Tp, size_t _Sz>
570    span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
571
572template<class _Tp, size_t _Sz>
573    span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
574
575#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
576template<ranges::contiguous_range _Range>
577    span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
578#endif
579
580#endif // _LIBCPP_STD_VER > 17
581
582_LIBCPP_END_NAMESPACE_STD
583
584_LIBCPP_POP_MACROS
585
586#endif // _LIBCPP_SPAN
587