xref: /llvm-project-15.0.7/libcxx/include/span (revision ff9e596b)
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)
174// This is a temporary workaround until we ship <ranges> -- we've unfortunately been
175// shipping <span> before its API was finalized, and we used to provide a constructor
176// from container types that had the requirements below. To avoid breaking code that
177// has started relying on the range-based constructor until we ship all of <ranges>,
178// we emulate the constructor requirements like this.
179template <class _Range, class _ElementType>
180concept __span_compatible_range =
181  !__is_std_span<remove_cvref_t<_Range>>::value  &&
182  !__is_std_array<remove_cvref_t<_Range>>::value &&
183  !is_array_v<remove_cvref_t<_Range>> &&
184  requires (_Range&& __r) {
185      data(std::forward<_Range>(__r));
186      size(std::forward<_Range>(__r));
187  } &&
188  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
189#else
190template <class _Range, class _ElementType>
191concept __span_compatible_range =
192  ranges::contiguous_range<_Range> &&
193  ranges::sized_range<_Range> &&
194  (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
195  !__is_std_span<remove_cvref_t<_Range>>::value  &&
196  !__is_std_array<remove_cvref_t<_Range>>::value &&
197  !is_array_v<remove_cvref_t<_Range>> &&
198  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
199#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
200
201template <typename _Tp, size_t _Extent>
202class _LIBCPP_TEMPLATE_VIS span {
203public:
204//  constants and types
205    using element_type           = _Tp;
206    using value_type             = remove_cv_t<_Tp>;
207    using size_type              = size_t;
208    using difference_type        = ptrdiff_t;
209    using pointer                = _Tp *;
210    using const_pointer          = const _Tp *;
211    using reference              = _Tp &;
212    using const_reference        = const _Tp &;
213#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
214    using iterator               = pointer;
215#else
216    using iterator               = __wrap_iter<pointer>;
217#endif
218    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
219
220    static constexpr size_type extent = _Extent;
221
222// [span.cons], span constructors, copy, assignment, and destructor
223    template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
224    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
225
226    constexpr span           (const span&) noexcept = default;
227    constexpr span& operator=(const span&) noexcept = default;
228
229    template <class _It,
230              enable_if_t<contiguous_iterator<_It> &&
231                              is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
232                          nullptr_t> = nullptr>
233    _LIBCPP_INLINE_VISIBILITY
234    constexpr explicit span(_It __first, size_type __count)
235        : __data{_VSTD::to_address(__first)} {
236      (void)__count;
237      _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
238    }
239
240    template <
241        class _It, class _End,
242        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
243                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
244                    nullptr_t> = nullptr>
245    _LIBCPP_INLINE_VISIBILITY
246    constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
247      (void)__last;
248      _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
249      _LIBCPP_ASSERT(__last - __first == _Extent,
250                     "invalid range in span's constructor (iterator, sentinel): last - first != extent");
251    }
252
253    _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}
254
255    template <class _OtherElementType,
256              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
257    _LIBCPP_INLINE_VISIBILITY
258    constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
259
260    template <class _OtherElementType,
261              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
262    _LIBCPP_INLINE_VISIBILITY
263    constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
264
265#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
266    template <class _Container>
267        requires __span_compatible_range<_Container, element_type>
268    _LIBCPP_INLINE_VISIBILITY
269    constexpr explicit span(_Container& __c) : __data{std::data(__c)} {
270      _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
271    }
272    template <class _Container>
273        requires __span_compatible_range<const _Container, element_type>
274    _LIBCPP_INLINE_VISIBILITY
275    constexpr explicit span(const _Container& __c) : __data{std::data(__c)} {
276      _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
277    }
278#else
279    template <__span_compatible_range<element_type> _Range>
280    _LIBCPP_INLINE_VISIBILITY
281    constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
282      _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
283    }
284#endif
285
286    template <class _OtherElementType>
287    _LIBCPP_INLINE_VISIBILITY
288        constexpr span(const span<_OtherElementType, _Extent>& __other,
289                       enable_if_t<
290                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
291                          nullptr_t> = nullptr)
292        : __data{__other.data()} {}
293
294    template <class _OtherElementType>
295    _LIBCPP_INLINE_VISIBILITY
296        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
297                       enable_if_t<
298                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
299                          nullptr_t> = nullptr) noexcept
300        : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
301
302
303//  ~span() noexcept = default;
304
305    template <size_t _Count>
306    _LIBCPP_INLINE_VISIBILITY
307    constexpr span<element_type, _Count> first() const noexcept
308    {
309        static_assert(_Count <= _Extent, "Count out of range in span::first()");
310        return span<element_type, _Count>{data(), _Count};
311    }
312
313    template <size_t _Count>
314    _LIBCPP_INLINE_VISIBILITY
315    constexpr span<element_type, _Count> last() const noexcept
316    {
317        static_assert(_Count <= _Extent, "Count out of range in span::last()");
318        return span<element_type, _Count>{data() + size() - _Count, _Count};
319    }
320
321    _LIBCPP_INLINE_VISIBILITY
322    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
323    {
324        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
325        return {data(), __count};
326    }
327
328    _LIBCPP_INLINE_VISIBILITY
329    constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
330    {
331        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
332        return {data() + size() - __count, __count};
333    }
334
335    template <size_t _Offset, size_t _Count = dynamic_extent>
336    _LIBCPP_INLINE_VISIBILITY
337    constexpr auto subspan() const noexcept
338        -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
339    {
340        static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
341        static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
342
343        using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
344        return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
345    }
346
347
348    _LIBCPP_INLINE_VISIBILITY
349    constexpr span<element_type, dynamic_extent>
350       subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
351    {
352        _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
353        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
354        if (__count == dynamic_extent)
355            return {data() + __offset, size() - __offset};
356        _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
357        return {data() + __offset, __count};
358    }
359
360    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return _Extent; }
361    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return _Extent * sizeof(element_type); }
362    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
363
364    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
365    {
366        _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
367        return __data[__idx];
368    }
369
370    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
371    {
372        _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
373        return __data[0];
374    }
375
376    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
377    {
378        _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
379        return __data[size()-1];
380    }
381
382    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
383
384// [span.iter], span iterator support
385    _LIBCPP_INLINE_VISIBILITY constexpr iterator                 begin() const noexcept { return iterator(data()); }
386    _LIBCPP_INLINE_VISIBILITY constexpr iterator                   end() const noexcept { return iterator(data() + size()); }
387    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
388    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
389
390    _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
391    { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
392
393    _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
394    { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
395
396private:
397    pointer    __data;
398
399};
400
401
402template <typename _Tp>
403class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
404private:
405
406public:
407//  constants and types
408    using element_type           = _Tp;
409    using value_type             = remove_cv_t<_Tp>;
410    using size_type              = size_t;
411    using difference_type        = ptrdiff_t;
412    using pointer                = _Tp *;
413    using const_pointer          = const _Tp *;
414    using reference              = _Tp &;
415    using const_reference        = const _Tp &;
416#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
417    using iterator               = pointer;
418#else
419    using iterator               = __wrap_iter<pointer>;
420#endif
421    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
422
423    static constexpr size_type extent = dynamic_extent;
424
425// [span.cons], span constructors, copy, assignment, and destructor
426    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
427
428    constexpr span           (const span&) noexcept = default;
429    constexpr span& operator=(const span&) noexcept = default;
430
431    template <class _It,
432              enable_if_t<contiguous_iterator<_It> &&
433                              is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
434                          nullptr_t> = nullptr>
435    _LIBCPP_INLINE_VISIBILITY
436    constexpr span(_It __first, size_type __count)
437        : __data{_VSTD::to_address(__first)}, __size{__count} {}
438
439    template <
440        class _It, class _End,
441        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
442                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
443                    nullptr_t> = nullptr>
444    _LIBCPP_INLINE_VISIBILITY
445    constexpr span(_It __first, _End __last)
446        : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
447
448    template <size_t _Sz>
449    _LIBCPP_INLINE_VISIBILITY
450    constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
451
452    template <class _OtherElementType, size_t _Sz,
453              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
454    _LIBCPP_INLINE_VISIBILITY
455    constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
456
457    template <class _OtherElementType, size_t _Sz,
458              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
459    _LIBCPP_INLINE_VISIBILITY
460    constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
461
462#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
463    template <class _Container>
464        requires __span_compatible_range<_Container, element_type>
465    _LIBCPP_INLINE_VISIBILITY
466    constexpr span(_Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
467    template <class _Container>
468        requires __span_compatible_range<const _Container, element_type>
469    _LIBCPP_INLINE_VISIBILITY
470    constexpr span(const _Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
471#else
472    template <__span_compatible_range<element_type> _Range>
473    _LIBCPP_INLINE_VISIBILITY
474    constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
475#endif
476
477    template <class _OtherElementType, size_t _OtherExtent>
478    _LIBCPP_INLINE_VISIBILITY
479        constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
480                       enable_if_t<
481                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
482                          nullptr_t> = nullptr) noexcept
483        : __data{__other.data()}, __size{__other.size()} {}
484
485//    ~span() noexcept = default;
486
487    template <size_t _Count>
488    _LIBCPP_INLINE_VISIBILITY
489    constexpr span<element_type, _Count> first() const noexcept
490    {
491        _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
492        return span<element_type, _Count>{data(), _Count};
493    }
494
495    template <size_t _Count>
496    _LIBCPP_INLINE_VISIBILITY
497    constexpr span<element_type, _Count> last() const noexcept
498    {
499        _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
500        return span<element_type, _Count>{data() + size() - _Count, _Count};
501    }
502
503    _LIBCPP_INLINE_VISIBILITY
504    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
505    {
506        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
507        return {data(), __count};
508    }
509
510    _LIBCPP_INLINE_VISIBILITY
511    constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
512    {
513        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
514        return {data() + size() - __count, __count};
515    }
516
517    template <size_t _Offset, size_t _Count = dynamic_extent>
518    _LIBCPP_INLINE_VISIBILITY
519    constexpr span<element_type, _Count> subspan() const noexcept
520    {
521        _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
522        _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
523        return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
524    }
525
526    constexpr span<element_type, dynamic_extent>
527    _LIBCPP_INLINE_VISIBILITY
528    subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
529    {
530        _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
531        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
532        if (__count == dynamic_extent)
533            return {data() + __offset, size() - __offset};
534        _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
535        return {data() + __offset, __count};
536    }
537
538    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return __size; }
539    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return __size * sizeof(element_type); }
540    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
541
542    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
543    {
544        _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
545        return __data[__idx];
546    }
547
548    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
549    {
550        _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
551        return __data[0];
552    }
553
554    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
555    {
556        _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
557        return __data[size()-1];
558    }
559
560
561    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
562
563// [span.iter], span iterator support
564    _LIBCPP_INLINE_VISIBILITY constexpr iterator                 begin() const noexcept { return iterator(data()); }
565    _LIBCPP_INLINE_VISIBILITY constexpr iterator                   end() const noexcept { return iterator(data() + size()); }
566    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
567    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
568
569    _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
570    { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
571
572    _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
573    { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
574
575private:
576    pointer   __data;
577    size_type __size;
578};
579
580template <class _Tp, size_t _Extent>
581inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
582
583template <class _ElementType, size_t _Extent>
584inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
585
586//  as_bytes & as_writable_bytes
587template <class _Tp, size_t _Extent>
588_LIBCPP_INLINE_VISIBILITY
589auto as_bytes(span<_Tp, _Extent> __s) noexcept
590-> decltype(__s.__as_bytes())
591{ return    __s.__as_bytes(); }
592
593template <class _Tp, size_t _Extent>
594_LIBCPP_INLINE_VISIBILITY
595auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
596-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
597{ return __s.__as_writable_bytes(); }
598
599#if _LIBCPP_STD_VER > 17
600template<contiguous_iterator _It, class _EndOrSize>
601    span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
602#endif // _LIBCPP_STD_VER > 17
603
604template<class _Tp, size_t _Sz>
605    span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
606
607template<class _Tp, size_t _Sz>
608    span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
609
610template<class _Tp, size_t _Sz>
611    span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
612
613#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
614template<ranges::contiguous_range _Range>
615    span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
616#endif
617
618#endif // _LIBCPP_STD_VER > 17
619
620_LIBCPP_END_NAMESPACE_STD
621
622_LIBCPP_POP_MACROS
623
624#endif // _LIBCPP_SPAN
625