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 constexpr span& operator=(const span& other) noexcept = default; 75 76 // [span.sub], span subviews 77 template <size_t Count> 78 constexpr span<element_type, Count> first() const; 79 template <size_t Count> 80 constexpr span<element_type, Count> last() const; 81 template <size_t Offset, size_t Count = dynamic_extent> 82 constexpr span<element_type, see below> subspan() const; 83 84 constexpr span<element_type, dynamic_extent> first(size_type count) const; 85 constexpr span<element_type, dynamic_extent> last(size_type count) const; 86 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const; 87 88 // [span.obs], span observers 89 constexpr size_type size() const noexcept; 90 constexpr size_type size_bytes() const noexcept; 91 [[nodiscard]] constexpr bool empty() const noexcept; 92 93 // [span.elem], span element access 94 constexpr reference operator[](size_type idx) const; 95 constexpr reference at(size_type idx) const; // since C++26 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> // all public C++ headers provide the assertion handler 131#include <__config> 132#include <__fwd/span.h> 133#include <__iterator/bounded_iter.h> 134#include <__iterator/concepts.h> 135#include <__iterator/iterator_traits.h> 136#include <__iterator/wrap_iter.h> 137#include <__memory/pointer_traits.h> 138#include <__ranges/concepts.h> 139#include <__ranges/data.h> 140#include <__ranges/enable_borrowed_range.h> 141#include <__ranges/enable_view.h> 142#include <__ranges/size.h> 143#include <__type_traits/is_convertible.h> 144#include <__type_traits/remove_cvref.h> 145#include <__type_traits/remove_reference.h> 146#include <__type_traits/type_identity.h> 147#include <__utility/forward.h> 148#include <array> // for array 149#include <cstddef> // for byte 150#include <stdexcept> 151#include <version> 152 153// standard-mandated includes 154 155// [iterator.range] 156#include <__iterator/access.h> 157#include <__iterator/data.h> 158#include <__iterator/empty.h> 159#include <__iterator/reverse_access.h> 160#include <__iterator/size.h> 161 162#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 163# pragma GCC system_header 164#endif 165 166_LIBCPP_PUSH_MACROS 167#include <__undef_macros> 168 169_LIBCPP_BEGIN_NAMESPACE_STD 170 171#if _LIBCPP_STD_VER >= 20 172 173template <class _Tp> 174struct __is_std_array : false_type {}; 175 176template <class _Tp, size_t _Sz> 177struct __is_std_array<array<_Tp, _Sz>> : true_type {}; 178 179template <class _Tp> 180struct __is_std_span : false_type {}; 181 182template <class _Tp, size_t _Sz> 183struct __is_std_span<span<_Tp, _Sz>> : true_type {}; 184 185template <class _Range, class _ElementType> 186concept __span_compatible_range = 187 ranges::contiguous_range<_Range> && // 188 ranges::sized_range<_Range> && // 189 (ranges::borrowed_range<_Range> || is_const_v<_ElementType>)&& // 190 !__is_std_span<remove_cvref_t<_Range>>::value && // 191 !__is_std_array<remove_cvref_t<_Range>>::value && // 192 !is_array_v<remove_cvref_t<_Range>> && // 193 is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>> (*)[], _ElementType (*)[]>; 194 195template <class _From, class _To> 196concept __span_array_convertible = is_convertible_v<_From (*)[], _To (*)[]>; 197 198template <class _It, class _Tp> 199concept __span_compatible_iterator = 200 contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>; 201 202template <class _Sentinel, class _It> 203concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>; 204 205template <typename _Tp, size_t _Extent> 206class _LIBCPP_TEMPLATE_VIS span { 207public: 208 // constants and types 209 using element_type = _Tp; 210 using value_type = remove_cv_t<_Tp>; 211 using size_type = size_t; 212 using difference_type = ptrdiff_t; 213 using pointer = _Tp*; 214 using const_pointer = const _Tp*; 215 using reference = _Tp&; 216 using const_reference = const _Tp&; 217# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS 218 using iterator = __bounded_iter<pointer>; 219# else 220 using iterator = __wrap_iter<pointer>; 221# endif 222 using reverse_iterator = std::reverse_iterator<iterator>; 223 224 static constexpr size_type extent = _Extent; 225 226 // [span.cons], span constructors, copy, assignment, and destructor 227 template <size_t _Sz = _Extent> 228 requires(_Sz == 0) 229 _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr} {} 230 231 constexpr span(const span&) noexcept = default; 232 constexpr span& operator=(const span&) noexcept = default; 233 234 template <__span_compatible_iterator<element_type> _It> 235 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, size_type __count) : __data_{std::to_address(__first)} { 236 (void)__count; 237 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __count, "size mismatch in span's constructor (iterator, len)"); 238 } 239 240 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End> 241 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, _End __last) : __data_{std::to_address(__first)} { 242 // [span.cons]/10 243 // Throws: When and what last - first throws. 244 [[maybe_unused]] auto __dist = __last - __first; 245 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__dist >= 0, "invalid range in span's constructor (iterator, sentinel)"); 246 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 247 __dist == _Extent, "invalid range in span's constructor (iterator, sentinel): last - first != extent"); 248 } 249 250 _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data_{__arr} {} 251 252 template <__span_array_convertible<element_type> _OtherElementType> 253 _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {} 254 255 template <class _OtherElementType> 256 requires __span_array_convertible<const _OtherElementType, element_type> 257 _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept 258 : __data_{__arr.data()} {} 259 260 template <__span_compatible_range<element_type> _Range> 261 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} { 262 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)"); 263 } 264 265 template <__span_array_convertible<element_type> _OtherElementType> 266 _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _Extent>& __other) : __data_{__other.data()} {} 267 268 template <__span_array_convertible<element_type> _OtherElementType> 269 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept 270 : __data_{__other.data()} { 271 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); 272 } 273 274 template <size_t _Count> 275 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> first() const noexcept { 276 static_assert(_Count <= _Extent, "span<T, N>::first<Count>(): Count out of range"); 277 return span<element_type, _Count>{data(), _Count}; 278 } 279 280 template <size_t _Count> 281 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> last() const noexcept { 282 static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range"); 283 return span<element_type, _Count>{data() + size() - _Count, _Count}; 284 } 285 286 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept { 287 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::first(count): count out of range"); 288 return {data(), __count}; 289 } 290 291 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept { 292 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::last(count): count out of range"); 293 return {data() + size() - __count, __count}; 294 } 295 296 template <size_t _Offset, size_t _Count = dynamic_extent> 297 _LIBCPP_HIDE_FROM_ABI constexpr auto subspan() const noexcept 298 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> { 299 static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range"); 300 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, 301 "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range"); 302 303 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>; 304 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; 305 } 306 307 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> 308 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept { 309 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T, N>::subspan(offset, count): offset out of range"); 310 if (__count == dynamic_extent) 311 return {data() + __offset, size() - __offset}; 312 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 313 __count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range"); 314 return {data() + __offset, __count}; 315 } 316 317 _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return _Extent; } 318 _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } 319 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return _Extent == 0; } 320 321 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept { 322 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T, N>::operator[](index): index out of range"); 323 return __data_[__idx]; 324 } 325 326# if _LIBCPP_STD_VER >= 26 327 _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const { 328 if (__index >= size()) 329 std::__throw_out_of_range("span"); 330 return __data_[__index]; 331 } 332# endif 333 334 _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept { 335 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::front() on empty span"); 336 return __data_[0]; 337 } 338 339 _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept { 340 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::back() on empty span"); 341 return __data_[size() - 1]; 342 } 343 344 _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; } 345 346 // [span.iter], span iterator support 347 _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept { 348# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS 349 return std::__make_bounded_iter(data(), data(), data() + size()); 350# else 351 return iterator(data()); 352# endif 353 } 354 _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept { 355# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS 356 return std::__make_bounded_iter(data() + size(), data(), data() + size()); 357# else 358 return iterator(data() + size()); 359# endif 360 } 361 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } 362 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } 363 364 _LIBCPP_HIDE_FROM_ABI span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept { 365 return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte*>(data()), size_bytes()}; 366 } 367 368 _LIBCPP_HIDE_FROM_ABI span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept { 369 return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte*>(data()), size_bytes()}; 370 } 371 372private: 373 pointer __data_; 374}; 375 376template <typename _Tp> 377class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { 378public: 379 // constants and types 380 using element_type = _Tp; 381 using value_type = remove_cv_t<_Tp>; 382 using size_type = size_t; 383 using difference_type = ptrdiff_t; 384 using pointer = _Tp*; 385 using const_pointer = const _Tp*; 386 using reference = _Tp&; 387 using const_reference = const _Tp&; 388# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS 389 using iterator = __bounded_iter<pointer>; 390# else 391 using iterator = __wrap_iter<pointer>; 392# endif 393 using reverse_iterator = std::reverse_iterator<iterator>; 394 395 static constexpr size_type extent = dynamic_extent; 396 397 // [span.cons], span constructors, copy, assignment, and destructor 398 _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr}, __size_{0} {} 399 400 constexpr span(const span&) noexcept = default; 401 constexpr span& operator=(const span&) noexcept = default; 402 403 template <__span_compatible_iterator<element_type> _It> 404 _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, size_type __count) 405 : __data_{std::to_address(__first)}, __size_{__count} {} 406 407 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End> 408 _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, _End __last) 409 : __data_(std::to_address(__first)), __size_(__last - __first) { 410 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)"); 411 } 412 413 template <size_t _Sz> 414 _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept 415 : __data_{__arr}, __size_{_Sz} {} 416 417 template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz> 418 _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept 419 : __data_{__arr.data()}, __size_{_Sz} {} 420 421 template <class _OtherElementType, size_t _Sz> 422 requires __span_array_convertible<const _OtherElementType, element_type> 423 _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept 424 : __data_{__arr.data()}, __size_{_Sz} {} 425 426 template <__span_compatible_range<element_type> _Range> 427 _LIBCPP_HIDE_FROM_ABI constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {} 428 429 template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent> 430 _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept 431 : __data_{__other.data()}, __size_{__other.size()} {} 432 433 template <size_t _Count> 434 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> first() const noexcept { 435 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::first<Count>(): Count out of range"); 436 return span<element_type, _Count>{data(), _Count}; 437 } 438 439 template <size_t _Count> 440 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> last() const noexcept { 441 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::last<Count>(): Count out of range"); 442 return span<element_type, _Count>{data() + size() - _Count, _Count}; 443 } 444 445 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept { 446 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::first(count): count out of range"); 447 return {data(), __count}; 448 } 449 450 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept { 451 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::last(count): count out of range"); 452 return {data() + size() - __count, __count}; 453 } 454 455 template <size_t _Offset, size_t _Count = dynamic_extent> 456 _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> subspan() const noexcept { 457 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range"); 458 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count == dynamic_extent || _Count <= size() - _Offset, 459 "span<T>::subspan<Offset, Count>(): Offset + Count out of range"); 460 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; 461 } 462 463 constexpr span<element_type, dynamic_extent> _LIBCPP_HIDE_FROM_ABI 464 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept { 465 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T>::subspan(offset, count): offset out of range"); 466 if (__count == dynamic_extent) 467 return {data() + __offset, size() - __offset}; 468 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 469 __count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range"); 470 return {data() + __offset, __count}; 471 } 472 473 _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return __size_; } 474 _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept { return __size_ * sizeof(element_type); } 475 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return __size_ == 0; } 476 477 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept { 478 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T>::operator[](index): index out of range"); 479 return __data_[__idx]; 480 } 481 482# if _LIBCPP_STD_VER >= 26 483 _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const { 484 if (__index >= size()) 485 std::__throw_out_of_range("span"); 486 return __data_[__index]; 487 } 488# endif 489 490 _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept { 491 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::front() on empty span"); 492 return __data_[0]; 493 } 494 495 _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept { 496 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::back() on empty span"); 497 return __data_[size() - 1]; 498 } 499 500 _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; } 501 502 // [span.iter], span iterator support 503 _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept { 504# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS 505 return std::__make_bounded_iter(data(), data(), data() + size()); 506# else 507 return iterator(data()); 508# endif 509 } 510 _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept { 511# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS 512 return std::__make_bounded_iter(data() + size(), data(), data() + size()); 513# else 514 return iterator(data() + size()); 515# endif 516 } 517 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } 518 _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } 519 520 _LIBCPP_HIDE_FROM_ABI span<const byte, dynamic_extent> __as_bytes() const noexcept { 521 return {reinterpret_cast<const byte*>(data()), size_bytes()}; 522 } 523 524 _LIBCPP_HIDE_FROM_ABI span<byte, dynamic_extent> __as_writable_bytes() const noexcept { 525 return {reinterpret_cast<byte*>(data()), size_bytes()}; 526 } 527 528private: 529 pointer __data_; 530 size_type __size_; 531}; 532 533template <class _Tp, size_t _Extent> 534inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true; 535 536template <class _ElementType, size_t _Extent> 537inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true; 538 539// as_bytes & as_writable_bytes 540template <class _Tp, size_t _Extent> 541_LIBCPP_HIDE_FROM_ABI auto as_bytes(span<_Tp, _Extent> __s) noexcept { 542 return __s.__as_bytes(); 543} 544 545template <class _Tp, size_t _Extent> 546 requires(!is_const_v<_Tp>) 547_LIBCPP_HIDE_FROM_ABI auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept { 548 return __s.__as_writable_bytes(); 549} 550 551# if _LIBCPP_STD_VER >= 20 552template <contiguous_iterator _It, class _EndOrSize> 553span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; 554# endif // _LIBCPP_STD_VER >= 20 555 556template <class _Tp, size_t _Sz> 557span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; 558 559template <class _Tp, size_t _Sz> 560span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; 561 562template <class _Tp, size_t _Sz> 563span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>; 564 565template <ranges::contiguous_range _Range> 566span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>; 567 568#endif // _LIBCPP_STD_VER >= 20 569 570_LIBCPP_END_NAMESPACE_STD 571 572_LIBCPP_POP_MACROS 573 574#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 575# include <concepts> 576# include <functional> 577# include <iterator> 578# include <type_traits> 579#endif 580 581#endif // _LIBCPP_SPAN 582