2 //===----------------------------------------------------------------------===//
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
8 //===---------------------------------------------------------------------===//
19 inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
22 concept integral-constant-like = // exposition only, since C++26
23 is_integral_v<decltype(T::value)> &&
24 !is_same_v<bool, remove_const_t<decltype(T::value)>> &&
25 convertible_to<T, decltype(T::value)> &&
26 equality_comparable_with<T, decltype(T::value)> &&
27 bool_constant<T() == T::value>::value &&
28 bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value;
31 constexpr size_t maybe-static-ext = dynamic_extent; // exposition only, since C++26
32 template<integral-constant-like T>
33 constexpr size_t maybe-static-ext<T> = {T::value};
35 // [views.span], class template span
36 template <class ElementType, size_t Extent = dynamic_extent>
39 template<class ElementType, size_t Extent>
40 inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
42 template<class ElementType, size_t Extent>
43 inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
45 // [span.objectrep], views of object representation
46 template <class ElementType, size_t Extent>
47 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
48 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
50 template <class ElementType, size_t Extent>
51 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
52 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
55 template <class ElementType, size_t Extent = dynamic_extent>
58 // constants and types
59 using element_type = ElementType;
60 using value_type = remove_cv_t<ElementType>;
61 using size_type = size_t;
62 using difference_type = ptrdiff_t;
63 using pointer = element_type*;
64 using const_pointer = const element_type*;
65 using reference = element_type&;
66 using const_reference = const element_type&;
67 using iterator = implementation-defined;
68 using reverse_iterator = std::reverse_iterator<iterator>;
69 static constexpr size_type extent = Extent;
71 // [span.cons], span constructors, copy, assignment, and destructor
72 constexpr span() noexcept;
74 constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
75 template <class It, class End>
76 constexpr explicit(Extent != dynamic_extent) span(It first, End last);
78 constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept;
80 constexpr span(array<value_type, N>& arr) noexcept;
82 constexpr span(const array<value_type, N>& arr) noexcept;
84 constexpr explicit(Extent != dynamic_extent) span(R&& r);
85 constexpr explicit(extent != dynamic_extent) span(std::initializer_list<value_type> il); // Since C++26
86 constexpr span(const span& other) noexcept = default;
87 template <class OtherElementType, size_t OtherExtent>
88 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
89 constexpr span& operator=(const span& other) noexcept = default;
91 // [span.sub], span subviews
92 template <size_t Count>
93 constexpr span<element_type, Count> first() const;
94 template <size_t Count>
95 constexpr span<element_type, Count> last() const;
96 template <size_t Offset, size_t Count = dynamic_extent>
97 constexpr span<element_type, see below> subspan() const;
99 constexpr span<element_type, dynamic_extent> first(size_type count) const;
100 constexpr span<element_type, dynamic_extent> last(size_type count) const;
101 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
103 // [span.obs], span observers
104 constexpr size_type size() const noexcept;
105 constexpr size_type size_bytes() const noexcept;
106 [[nodiscard]] constexpr bool empty() const noexcept;
108 // [span.elem], span element access
109 constexpr reference operator[](size_type idx) const;
110 constexpr reference at(size_type idx) const; // since C++26
111 constexpr reference front() const;
112 constexpr reference back() const;
113 constexpr pointer data() const noexcept;
115 // [span.iterators], span iterator support
116 constexpr iterator begin() const noexcept;
117 constexpr iterator end() const noexcept;
118 constexpr reverse_iterator rbegin() const noexcept;
119 constexpr reverse_iterator rend() const noexcept;
122 pointer data_; // exposition only
123 size_type size_; // exposition only
126 template<class It, class EndOrSize>
127 span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; // until C++26
128 template<class It, class EndOrSize>
129 span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<It>>, maybe-static-ext<EndOrSize>>; // since C++26
131 template<class T, size_t N>
132 span(T (&)[N]) -> span<T, N>;
134 template<class T, size_t N>
135 span(array<T, N>&) -> span<T, N>;
137 template<class T, size_t N>
138 span(const array<T, N>&) -> span<const T, N>;
141 span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
148 #include <__concepts/convertible_to.h>
149 #include <__concepts/equality_comparable.h>
151 #include <__cstddef/byte.h>
152 #include <__cstddef/ptrdiff_t.h>
153 #include <__fwd/array.h>
154 #include <__fwd/span.h>
155 #include <__iterator/bounded_iter.h>
156 #include <__iterator/concepts.h>
157 #include <__iterator/iterator_traits.h>
158 #include <__iterator/reverse_iterator.h>
159 #include <__iterator/wrap_iter.h>
160 #include <__memory/pointer_traits.h>
161 #include <__ranges/concepts.h>
162 #include <__ranges/data.h>
163 #include <__ranges/enable_borrowed_range.h>
164 #include <__ranges/enable_view.h>
165 #include <__ranges/size.h>
166 #include <__type_traits/integral_constant.h>
167 #include <__type_traits/is_array.h>
168 #include <__type_traits/is_const.h>
169 #include <__type_traits/is_convertible.h>
170 #include <__type_traits/is_integral.h>
171 #include <__type_traits/is_same.h>
172 #include <__type_traits/remove_const.h>
173 #include <__type_traits/remove_cv.h>
174 #include <__type_traits/remove_cvref.h>
175 #include <__type_traits/remove_reference.h>
176 #include <__type_traits/type_identity.h>
177 #include <__utility/forward.h>
178 #include <initializer_list>
182 // standard-mandated includes
185 #include <__iterator/access.h>
186 #include <__iterator/data.h>
187 #include <__iterator/empty.h>
188 #include <__iterator/reverse_access.h>
189 #include <__iterator/size.h>
191 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
192 # pragma GCC system_header
196 #include <__undef_macros>
198 _LIBCPP_BEGIN_NAMESPACE_STD
200 #if _LIBCPP_STD_VER >= 20
203 struct __is_std_span : false_type {};
205 template <class _Tp, size_t _Sz>
206 struct __is_std_span<span<_Tp, _Sz>> : true_type {};
208 template <class _Range, class _ElementType>
209 concept __span_compatible_range =
210 !__is_std_span<remove_cvref_t<_Range>>::value && //
211 ranges::contiguous_range<_Range> && //
212 ranges::sized_range<_Range> && //
213 (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && //
214 !__is_std_array_v<remove_cvref_t<_Range>> && //
215 !is_array_v<remove_cvref_t<_Range>> && //
216 is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>> (*)[], _ElementType (*)[]>;
218 template <class _From, class _To>
219 concept __span_array_convertible = is_convertible_v<_From (*)[], _To (*)[]>;
221 template <class _It, class _Tp>
222 concept __span_compatible_iterator =
223 contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>;
225 template <class _Sentinel, class _It>
226 concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>;
228 template <typename _Tp, size_t _Extent>
229 class _LIBCPP_TEMPLATE_VIS span {
231 // constants and types
232 using element_type = _Tp;
233 using value_type = remove_cv_t<_Tp>;
234 using size_type = size_t;
235 using difference_type = ptrdiff_t;
236 using pointer = _Tp*;
237 using const_pointer = const _Tp*;
238 using reference = _Tp&;
239 using const_reference = const _Tp&;
240 # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
241 using iterator = __bounded_iter<pointer>;
243 using iterator = __wrap_iter<pointer>;
245 using reverse_iterator = std::reverse_iterator<iterator>;
247 static constexpr size_type extent = _Extent;
249 // [span.cons], span constructors, copy, assignment, and destructor
250 template <size_t _Sz = _Extent>
252 _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr} {}
254 # if _LIBCPP_STD_VER >= 26
255 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(std::initializer_list<value_type> __il)
256 requires is_const_v<element_type>
257 : __data_{__il.begin()} {
258 _LIBCPP_ASSERT_VALID_INPUT_RANGE(
259 _Extent == __il.size(), "Size mismatch in span's constructor _Extent != __il.size().");
263 constexpr span(const span&) noexcept = default;
264 constexpr span& operator=(const span&) noexcept = default;
266 template <__span_compatible_iterator<element_type> _It>
267 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, size_type __count) : __data_{std::to_address(__first)} {
269 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
272 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
273 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, _End __last) : __data_{std::to_address(__first)} {
275 // Throws: When and what last - first throws.
276 [[maybe_unused]] auto __dist = __last - __first;
277 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__dist >= 0, "invalid range in span's constructor (iterator, sentinel)");
278 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
279 __dist == _Extent, "invalid range in span's constructor (iterator, sentinel): last - first != extent");
282 _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data_{__arr} {}
284 template <__span_array_convertible<element_type> _OtherElementType>
285 _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {}
287 template <class _OtherElementType>
288 requires __span_array_convertible<const _OtherElementType, element_type>
289 _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept
290 : __data_{__arr.data()} {}
292 template <__span_compatible_range<element_type> _Range>
293 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} {
294 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
297 template <__span_array_convertible<element_type> _OtherElementType>
298 _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _Extent>& __other) noexcept
299 : __data_{__other.data()} {}
301 template <__span_array_convertible<element_type> _OtherElementType>
302 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept
303 : __data_{__other.data()} {
304 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __other.size(), "size mismatch in span's constructor (other span)");
307 template <size_t _Count>
308 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> first() const noexcept {
309 static_assert(_Count <= _Extent, "span<T, N>::first<Count>(): Count out of range");
310 return span<element_type, _Count>{data(), _Count};
313 template <size_t _Count>
314 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> last() const noexcept {
315 static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range");
316 return span<element_type, _Count>{data() + size() - _Count, _Count};
319 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept {
320 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::first(count): count out of range");
321 return {data(), __count};
324 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept {
325 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::last(count): count out of range");
326 return {data() + size() - __count, __count};
329 template <size_t _Offset, size_t _Count = dynamic_extent>
330 _LIBCPP_HIDE_FROM_ABI constexpr auto
331 subspan() const noexcept -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> {
332 static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range");
333 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset,
334 "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range");
336 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
337 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
340 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent>
341 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept {
342 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T, N>::subspan(offset, count): offset out of range");
343 if (__count == dynamic_extent)
344 return {data() + __offset, size() - __offset};
345 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
346 __count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range");
347 return {data() + __offset, __count};
350 _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return _Extent; }
351 _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
352 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return _Extent == 0; }
354 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept {
355 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T, N>::operator[](index): index out of range");
356 return __data_[__idx];
359 # if _LIBCPP_STD_VER >= 26
360 _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
361 if (__index >= size())
362 std::__throw_out_of_range("span");
363 return __data_[__index];
367 _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept {
368 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::front() on empty span");
372 _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept {
373 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::back() on empty span");
374 return __data_[size() - 1];
377 _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; }
379 // [span.iter], span iterator support
380 _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept {
381 # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
382 return std::__make_bounded_iter(data(), data(), data() + size());
384 return iterator(data());
387 _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept {
388 # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
389 return std::__make_bounded_iter(data() + size(), data(), data() + size());
391 return iterator(data() + size());
394 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
395 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
397 _LIBCPP_HIDE_FROM_ABI span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept {
398 return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte*>(data()), size_bytes()};
401 _LIBCPP_HIDE_FROM_ABI span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept {
402 return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte*>(data()), size_bytes()};
409 template <typename _Tp>
410 class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
412 // constants and types
413 using element_type = _Tp;
414 using value_type = remove_cv_t<_Tp>;
415 using size_type = size_t;
416 using difference_type = ptrdiff_t;
417 using pointer = _Tp*;
418 using const_pointer = const _Tp*;
419 using reference = _Tp&;
420 using const_reference = const _Tp&;
421 # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
422 using iterator = __bounded_iter<pointer>;
424 using iterator = __wrap_iter<pointer>;
426 using reverse_iterator = std::reverse_iterator<iterator>;
428 static constexpr size_type extent = dynamic_extent;
430 // [span.cons], span constructors, copy, assignment, and destructor
431 _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr}, __size_{0} {}
433 # if _LIBCPP_STD_VER >= 26
434 _LIBCPP_HIDE_FROM_ABI constexpr span(std::initializer_list<value_type> __il)
435 requires is_const_v<element_type>
436 : __data_{__il.begin()}, __size_{__il.size()} {}
439 constexpr span(const span&) noexcept = default;
440 constexpr span& operator=(const span&) noexcept = default;
442 template <__span_compatible_iterator<element_type> _It>
443 _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, size_type __count)
444 : __data_{std::to_address(__first)}, __size_{__count} {}
446 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
447 _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, _End __last)
448 : __data_(std::to_address(__first)), __size_(__last - __first) {
449 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)");
452 template <size_t _Sz>
453 _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept
454 : __data_{__arr}, __size_{_Sz} {}
456 template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz>
457 _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept
458 : __data_{__arr.data()}, __size_{_Sz} {}
460 template <class _OtherElementType, size_t _Sz>
461 requires __span_array_convertible<const _OtherElementType, element_type>
462 _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept
463 : __data_{__arr.data()}, __size_{_Sz} {}
465 template <__span_compatible_range<element_type> _Range>
466 _LIBCPP_HIDE_FROM_ABI constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {}
468 template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent>
469 _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept
470 : __data_{__other.data()}, __size_{__other.size()} {}
472 template <size_t _Count>
473 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> first() const noexcept {
474 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::first<Count>(): Count out of range");
475 return span<element_type, _Count>{data(), _Count};
478 template <size_t _Count>
479 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> last() const noexcept {
480 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::last<Count>(): Count out of range");
481 return span<element_type, _Count>{data() + size() - _Count, _Count};
484 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept {
485 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::first(count): count out of range");
486 return {data(), __count};
489 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept {
490 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::last(count): count out of range");
491 return {data() + size() - __count, __count};
494 template <size_t _Offset, size_t _Count = dynamic_extent>
495 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> subspan() const noexcept {
496 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range");
497 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count == dynamic_extent || _Count <= size() - _Offset,
498 "span<T>::subspan<Offset, Count>(): Offset + Count out of range");
499 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
502 constexpr span<element_type, dynamic_extent> _LIBCPP_HIDE_FROM_ABI
503 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept {
504 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T>::subspan(offset, count): offset out of range");
505 if (__count == dynamic_extent)
506 return {data() + __offset, size() - __offset};
507 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
508 __count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range");
509 return {data() + __offset, __count};
512 _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return __size_; }
513 _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept { return __size_ * sizeof(element_type); }
514 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return __size_ == 0; }
516 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept {
517 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T>::operator[](index): index out of range");
518 return __data_[__idx];
521 # if _LIBCPP_STD_VER >= 26
522 _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
523 if (__index >= size())
524 std::__throw_out_of_range("span");
525 return __data_[__index];
529 _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept {
530 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::front() on empty span");
534 _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept {
535 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::back() on empty span");
536 return __data_[size() - 1];
539 _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; }
541 // [span.iter], span iterator support
542 _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept {
543 # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
544 return std::__make_bounded_iter(data(), data(), data() + size());
546 return iterator(data());
549 _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept {
550 # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
551 return std::__make_bounded_iter(data() + size(), data(), data() + size());
553 return iterator(data() + size());
556 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
557 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
559 _LIBCPP_HIDE_FROM_ABI span<const byte, dynamic_extent> __as_bytes() const noexcept {
560 return {reinterpret_cast<const byte*>(data()), size_bytes()};
563 _LIBCPP_HIDE_FROM_ABI span<byte, dynamic_extent> __as_writable_bytes() const noexcept {
564 return {reinterpret_cast<byte*>(data()), size_bytes()};
572 template <class _Tp, size_t _Extent>
573 inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
575 template <class _ElementType, size_t _Extent>
576 inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
578 // as_bytes & as_writable_bytes
579 template <class _Tp, size_t _Extent>
580 _LIBCPP_HIDE_FROM_ABI auto as_bytes(span<_Tp, _Extent> __s) noexcept {
581 return __s.__as_bytes();
584 template <class _Tp, size_t _Extent>
585 requires(!is_const_v<_Tp>)
586 _LIBCPP_HIDE_FROM_ABI auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept {
587 return __s.__as_writable_bytes();
590 # if _LIBCPP_STD_VER >= 26
592 concept __integral_constant_like =
593 is_integral_v<decltype(_Tp::value)> && !is_same_v<bool, remove_const_t<decltype(_Tp::value)>> &&
594 convertible_to<_Tp, decltype(_Tp::value)> && equality_comparable_with<_Tp, decltype(_Tp::value)> &&
595 bool_constant<_Tp() == _Tp::value>::value &&
596 bool_constant<static_cast<decltype(_Tp::value)>(_Tp()) == _Tp::value>::value;
599 inline constexpr size_t __maybe_static_ext = dynamic_extent;
601 template <__integral_constant_like _Tp>
602 inline constexpr size_t __maybe_static_ext<_Tp> = {_Tp::value};
604 template <contiguous_iterator _It, class _EndOrSize>
605 span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>, __maybe_static_ext<_EndOrSize>>;
607 template <contiguous_iterator _It, class _EndOrSize>
608 span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
611 template <class _Tp, size_t _Sz>
612 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
614 template <class _Tp, size_t _Sz>
615 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
617 template <class _Tp, size_t _Sz>
618 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
620 template <ranges::contiguous_range _Range>
621 span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
623 #endif // _LIBCPP_STD_VER >= 20
625 _LIBCPP_END_NAMESPACE_STD
629 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
632 # include <functional>
634 # include <type_traits>
637 #endif // _LIBCPP_SPAN