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