Add documentation for to_integer(byte) (#1144)
[GSL.git] / include / gsl / span
blob0de293238ba23f150ae2c82e0b80f753a3697c8f
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
15 ///////////////////////////////////////////////////////////////////////////////
17 #ifndef GSL_SPAN_H
18 #define GSL_SPAN_H
20 #include "assert"   // for Expects
21 #include "byte"     // for byte
22 #include "span_ext" // for span specialization of gsl::at and other span-related extensions
23 #include "util"     // for narrow_cast
25 #include <array>       // for array
26 #include <cstddef>     // for ptrdiff_t, size_t, nullptr_t
27 #include <iterator>    // for reverse_iterator, distance, random_access_...
28 #include <memory>      // for pointer_traits
29 #include <type_traits> // for enable_if_t, declval, is_convertible, inte...
31 #if defined(__has_include) && __has_include(<version>)
32 #include <version>
33 #endif
35 #if defined(_MSC_VER) && !defined(__clang__)
36 #pragma warning(push)
38 // turn off some warnings that are noisy about our Expects statements
39 #pragma warning(disable : 4127) // conditional expression is constant
40 #pragma warning(                                                                                   \
41     disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
42 #pragma warning(disable : 4702) // unreachable code
44 // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
45 #pragma warning(disable : 26495) // uninitialized member when constructor calls constructor
46 #pragma warning(disable : 26446) // parser bug does not allow attributes on some templates
48 #endif // _MSC_VER
50 // See if we have enough C++17 power to use a static constexpr data member
51 // without needing an out-of-line definition
52 #if !(defined(__cplusplus) && (__cplusplus >= 201703L))
53 #define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
54 #endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
56 // GCC 7 does not like the signed unsigned mismatch (size_t ptrdiff_t)
57 // While there is a conversion from signed to unsigned, it happens at
58 // compiletime, so the compiler wouldn't have to warn indiscriminately, but
59 // could check if the source value actually doesn't fit into the target type
60 // and only warn in those cases.
61 #if defined(__GNUC__) && __GNUC__ > 6
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wsign-conversion"
64 #endif
66 // Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
67 #if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
68 #pragma clang diagnostic push
69 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
70 #endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
72 namespace gsl
75 // implementation details
76 namespace details
78     template <class T>
79     struct is_span_oracle : std::false_type
80     {
81     };
83     template <class ElementType, std::size_t Extent>
84     struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
85     {
86     };
88     template <class T>
89     struct is_span : public is_span_oracle<std::remove_cv_t<T>>
90     {
91     };
93     template <class T>
94     struct is_std_array_oracle : std::false_type
95     {
96     };
98     template <class ElementType, std::size_t Extent>
99     struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
100     {
101     };
103     template <class T>
104     struct is_std_array : is_std_array_oracle<std::remove_cv_t<T>>
105     {
106     };
108     template <std::size_t From, std::size_t To>
109     struct is_allowed_extent_conversion
110         : std::integral_constant<bool, From == To || To == dynamic_extent>
111     {
112     };
114     template <class From, class To>
115     struct is_allowed_element_type_conversion
116         : std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
117     {
118     };
120     template <class Type>
121     class span_iterator
122     {
123     public:
124 #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts))
125         using iterator_concept = std::contiguous_iterator_tag;
126 #endif // __cpp_lib_ranges
127         using iterator_category = std::random_access_iterator_tag;
128         using value_type = std::remove_cv_t<Type>;
129         using difference_type = std::ptrdiff_t;
130         using pointer = Type*;
131         using reference = Type&;
133 #ifdef _MSC_VER
134         using _Unchecked_type = pointer;
135         using _Prevent_inheriting_unwrap = span_iterator;
136 #endif // _MSC_VER
137         constexpr span_iterator() = default;
139         constexpr span_iterator(pointer begin, pointer end, pointer current)
140             : begin_(begin), end_(end), current_(current)
141         {}
143         constexpr operator span_iterator<const Type>() const noexcept
144         {
145             return {begin_, end_, current_};
146         }
148         constexpr reference operator*() const noexcept
149         {
150             Expects(begin_ && end_);
151             Expects(begin_ <= current_ && current_ < end_);
152             return *current_;
153         }
155         constexpr pointer operator->() const noexcept
156         {
157             Expects(begin_ && end_);
158             Expects(begin_ <= current_ && current_ < end_);
159             return current_;
160         }
161         constexpr span_iterator& operator++() noexcept
162         {
163             Expects(begin_ && current_ && end_);
164             Expects(current_ < end_);
165             // clang-format off
166             GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
167             // clang-format on
168             ++current_;
169             return *this;
170         }
172         constexpr span_iterator operator++(int) noexcept
173         {
174             span_iterator ret = *this;
175             ++*this;
176             return ret;
177         }
179         constexpr span_iterator& operator--() noexcept
180         {
181             Expects(begin_ && end_);
182             Expects(begin_ < current_);
183             --current_;
184             return *this;
185         }
187         constexpr span_iterator operator--(int) noexcept
188         {
189             span_iterator ret = *this;
190             --*this;
191             return ret;
192         }
194         constexpr span_iterator& operator+=(const difference_type n) noexcept
195         {
196             if (n != 0) Expects(begin_ && current_ && end_);
197             if (n > 0) Expects(end_ - current_ >= n);
198             if (n < 0) Expects(current_ - begin_ >= -n);
199             // clang-format off
200             GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
201             // clang-format on
202             current_ += n;
203             return *this;
204         }
206         constexpr span_iterator operator+(const difference_type n) const noexcept
207         {
208             span_iterator ret = *this;
209             ret += n;
210             return ret;
211         }
213         friend constexpr span_iterator operator+(const difference_type n,
214                                                  const span_iterator& rhs) noexcept
215         {
216             return rhs + n;
217         }
219         constexpr span_iterator& operator-=(const difference_type n) noexcept
220         {
221             if (n != 0) Expects(begin_ && current_ && end_);
222             if (n > 0) Expects(current_ - begin_ >= n);
223             if (n < 0) Expects(end_ - current_ >= -n);
224             GSL_SUPPRESS(bounds .1)
225             current_ -= n;
226             return *this;
227         }
229         constexpr span_iterator operator-(const difference_type n) const noexcept
230         {
231             span_iterator ret = *this;
232             ret -= n;
233             return ret;
234         }
236         template <
237             class Type2,
238             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
239         constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
240         {
241             Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
242             return current_ - rhs.current_;
243         }
245         constexpr reference operator[](const difference_type n) const noexcept
246         {
247             return *(*this + n);
248         }
250         template <
251             class Type2,
252             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
253         constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
254         {
255             Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
256             return current_ == rhs.current_;
257         }
259         template <
260             class Type2,
261             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
262         constexpr bool operator!=(const span_iterator<Type2>& rhs) const noexcept
263         {
264             return !(*this == rhs);
265         }
267         template <
268             class Type2,
269             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
270         constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
271         {
272             Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
273             return current_ < rhs.current_;
274         }
276         template <
277             class Type2,
278             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
279         constexpr bool operator>(const span_iterator<Type2>& rhs) const noexcept
280         {
281             return rhs < *this;
282         }
284         template <
285             class Type2,
286             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
287         constexpr bool operator<=(const span_iterator<Type2>& rhs) const noexcept
288         {
289             return !(rhs < *this);
290         }
292         template <
293             class Type2,
294             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
295         constexpr bool operator>=(const span_iterator<Type2>& rhs) const noexcept
296         {
297             return !(*this < rhs);
298         }
300 #ifdef _MSC_VER
301         // MSVC++ iterator debugging support; allows STL algorithms in 15.8+
302         // to unwrap span_iterator to a pointer type after a range check in STL
303         // algorithm calls
304         friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
305         { // test that [lhs, rhs) forms a valid range inside an STL algorithm
306             Expects(lhs.begin_ == rhs.begin_ // range spans have to match
307                     && lhs.end_ == rhs.end_ &&
308                     lhs.current_ <= rhs.current_); // range must not be transposed
309         }
311         constexpr void _Verify_offset(const difference_type n) const noexcept
312         { // test that *this + n is within the range of this call
313             if (n != 0) Expects(begin_ && current_ && end_);
314             if (n > 0) Expects(end_ - current_ >= n);
315             if (n < 0) Expects(current_ - begin_ >= -n);
316         }
318         // clang-format off
319         GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
320         // clang-format on
321         constexpr pointer _Unwrapped() const noexcept
322         { // after seeking *this to a high water mark, or using one of the
323             // _Verify_xxx functions above, unwrap this span_iterator to a raw
324             // pointer
325             return current_;
326         }
328         // Tell the STL that span_iterator should not be unwrapped if it can't
329         // validate in advance, even in release / optimized builds:
330 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
331         static constexpr const bool _Unwrap_when_unverified = false;
332 #else
333         static constexpr bool _Unwrap_when_unverified = false;
334 #endif
335         // clang-format off
336         GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
337         // clang-format on
338         constexpr void _Seek_to(const pointer p) noexcept
339         { // adjust the position of *this to previously verified location p
340             // after _Unwrapped
341             current_ = p;
342         }
343 #endif
345         pointer begin_ = nullptr;
346         pointer end_ = nullptr;
347         pointer current_ = nullptr;
349         template <typename Ptr>
350         friend struct std::pointer_traits;
351     };
352 }} // namespace gsl::details
354 namespace std
356 template <class Type>
357 struct pointer_traits<::gsl::details::span_iterator<Type>>
359     using pointer = ::gsl::details::span_iterator<Type>;
360     using element_type = Type;
361     using difference_type = ptrdiff_t;
363     static constexpr element_type* to_address(const pointer i) noexcept { return i.current_; }
365 } // namespace std
367 namespace gsl { namespace details {
368     template <std::size_t Ext>
369     class extent_type
370     {
371     public:
372         using size_type = std::size_t;
374         constexpr extent_type() noexcept = default;
376         constexpr explicit extent_type(extent_type<dynamic_extent>);
378         constexpr explicit extent_type(size_type size) { Expects(size == Ext); }
380         constexpr size_type size() const noexcept { return Ext; }
382     private:
383 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
384         static constexpr const size_type size_ = Ext; // static size equal to Ext
385 #else
386         static constexpr size_type size_ = Ext; // static size equal to Ext
387 #endif
388     };
390     template <>
391     class extent_type<dynamic_extent>
392     {
393     public:
394         using size_type = std::size_t;
396         template <size_type Other>
397         constexpr explicit extent_type(extent_type<Other> ext) : size_(ext.size())
398         {}
400         constexpr explicit extent_type(size_type size) : size_(size)
401         {
402             Expects(size != dynamic_extent);
403         }
405         constexpr size_type size() const noexcept { return size_; }
407     private:
408         size_type size_;
409     };
411     template <std::size_t Ext>
412     constexpr extent_type<Ext>::extent_type(extent_type<dynamic_extent> ext)
413     {
414         Expects(ext.size() == Ext);
415     }
417     template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
418     struct calculate_subspan_type
419     {
420         using type = span<ElementType, Count != dynamic_extent
421                                            ? Count
422                                            : (Extent != dynamic_extent ? Extent - Offset : Extent)>;
423     };
424 } // namespace details
426 // [span], class template span
427 template <class ElementType, std::size_t Extent>
428 class span
430 public:
431     // constants and types
432     using element_type = ElementType;
433     using value_type = std::remove_cv_t<ElementType>;
434     using size_type = std::size_t;
435     using pointer = element_type*;
436     using const_pointer = const element_type*;
437     using reference = element_type&;
438     using const_reference = const element_type&;
439     using difference_type = std::ptrdiff_t;
441     using iterator = details::span_iterator<ElementType>;
442     using reverse_iterator = std::reverse_iterator<iterator>;
444 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
445     static constexpr const size_type extent{Extent};
446 #else
447     static constexpr size_type extent{Extent};
448 #endif
450     // [span.cons], span constructors, copy, assignment, and destructor
451     template <bool Dependent = false,
452               // "Dependent" is needed to make "std::enable_if_t<Dependent || Extent == 0 || Extent
453               // == dynamic_extent>" SFINAE, since "std::enable_if_t<Extent == 0 || Extent ==
454               // dynamic_extent>" is ill-formed when Extent is greater than 0.
455               class = std::enable_if_t<(Dependent ||
456                                         details::is_allowed_extent_conversion<0, Extent>::value)>>
457     constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
458     {}
460     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
461     constexpr explicit span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
462     {
463         Expects(count == Extent);
464     }
466     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
467     constexpr span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
468     {}
470     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
471     constexpr explicit span(pointer firstElem, pointer lastElem) noexcept
472         : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
473     {
474         Expects(lastElem - firstElem == static_cast<difference_type>(Extent));
475     }
477     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
478     constexpr span(pointer firstElem, pointer lastElem) noexcept
479         : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
480     {}
482     template <std::size_t N,
483               std::enable_if_t<details::is_allowed_extent_conversion<N, Extent>::value, int> = 0>
484     constexpr span(element_type (&arr)[N]) noexcept
485         : storage_(KnownNotNull{arr}, details::extent_type<N>())
486     {}
488     template <
489         class T, std::size_t N,
490         std::enable_if_t<(details::is_allowed_extent_conversion<N, Extent>::value &&
491                           details::is_allowed_element_type_conversion<T, element_type>::value),
492                          int> = 0>
493     constexpr span(std::array<T, N>& arr) noexcept
494         : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
495     {}
497     template <class T, std::size_t N,
498               std::enable_if_t<
499                   (details::is_allowed_extent_conversion<N, Extent>::value &&
500                    details::is_allowed_element_type_conversion<const T, element_type>::value),
501                   int> = 0>
502     constexpr span(const std::array<T, N>& arr) noexcept
503         : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
504     {}
506     // NB: the SFINAE on these constructors uses .data() as an incomplete/imperfect proxy for the
507     // requirement on Container to be a contiguous sequence container.
508     template <std::size_t MyExtent = Extent, class Container,
509               std::enable_if_t<
510                   MyExtent != dynamic_extent && !details::is_span<Container>::value &&
511                       !details::is_std_array<Container>::value &&
512                       std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
513                       std::is_convertible<
514                           std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
515                           element_type (*)[]>::value,
516                   int> = 0>
517     constexpr explicit span(Container& cont) noexcept : span(cont.data(), cont.size())
518     {}
520     template <std::size_t MyExtent = Extent, class Container,
521               std::enable_if_t<
522                   MyExtent == dynamic_extent && !details::is_span<Container>::value &&
523                       !details::is_std_array<Container>::value &&
524                       std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
525                       std::is_convertible<
526                           std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
527                           element_type (*)[]>::value,
528                   int> = 0>
529     constexpr span(Container& cont) noexcept : span(cont.data(), cont.size())
530     {}
532     template <
533         std::size_t MyExtent = Extent, class Container,
534         std::enable_if_t<
535             MyExtent != dynamic_extent && std::is_const<element_type>::value &&
536                 !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
537                 std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
538                 std::is_convertible<
539                     std::remove_pointer_t<decltype(std::declval<const Container&>().data())> (*)[],
540                     element_type (*)[]>::value,
541             int> = 0>
542     constexpr explicit span(const Container& cont) noexcept : span(cont.data(), cont.size())
543     {}
545     template <
546         std::size_t MyExtent = Extent, class Container,
547         std::enable_if_t<
548             MyExtent == dynamic_extent && std::is_const<element_type>::value &&
549                 !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
550                 std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
551                 std::is_convertible<
552                     std::remove_pointer_t<decltype(std::declval<const Container&>().data())> (*)[],
553                     element_type (*)[]>::value,
554             int> = 0>
555     constexpr span(const Container& cont) noexcept : span(cont.data(), cont.size())
556     {}
558     constexpr span(const span& other) noexcept = default;
560     template <class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
561               std::enable_if_t<(MyExtent == dynamic_extent || MyExtent == OtherExtent) &&
562                                    details::is_allowed_element_type_conversion<OtherElementType,
563                                                                                element_type>::value,
564                                int> = 0>
565     constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
566         : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
567     {}
569     template <class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
570               std::enable_if_t<MyExtent != dynamic_extent && OtherExtent == dynamic_extent &&
571                                    details::is_allowed_element_type_conversion<OtherElementType,
572                                                                                element_type>::value,
573                                int> = 0>
574     constexpr explicit span(const span<OtherElementType, OtherExtent>& other) noexcept
575         : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
576     {}
578     ~span() noexcept = default;
579     constexpr span& operator=(const span& other) noexcept = default;
581     // [span.sub], span subviews
582     template <std::size_t Count>
583     constexpr span<element_type, Count> first() const noexcept
584     {
585         Expects(Count <= size());
586         return span<element_type, Count>{data(), Count};
587     }
589     template <std::size_t Count>
590     // clang-format off
591     GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
592         // clang-format on
593         constexpr span<element_type, Count> last() const noexcept
594     {
595         Expects(Count <= size());
596         return span<element_type, Count>{data() + (size() - Count), Count};
597     }
599     template <std::size_t Offset, std::size_t Count = dynamic_extent>
600     // clang-format off
601     GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
602         // clang-format on
603         constexpr auto subspan() const noexcept ->
604         typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
605     {
606         Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
607         using type =
608             typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type;
609         return type{data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
610     }
612     constexpr span<element_type, dynamic_extent> first(size_type count) const noexcept
613     {
614         Expects(count <= size());
615         return {data(), count};
616     }
618     constexpr span<element_type, dynamic_extent> last(size_type count) const noexcept
619     {
620         Expects(count <= size());
621         return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
622     }
624     constexpr span<element_type, dynamic_extent>
625     subspan(size_type offset, size_type count = dynamic_extent) const noexcept
626     {
627         return make_subspan(offset, count, subspan_selector<Extent>{});
628     }
630     // [span.obs], span observers
631     constexpr size_type size() const noexcept { return storage_.size(); }
633     constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); }
635     constexpr bool empty() const noexcept { return size() == 0; }
637     // [span.elem], span element access
638     // clang-format off
639     GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
640     // clang-format on
641     constexpr reference operator[](size_type idx) const noexcept
642     {
643         Expects(idx < size());
644         return data()[idx];
645     }
647     constexpr reference front() const noexcept
648     {
649         Expects(size() > 0);
650         return data()[0];
651     }
653     constexpr reference back() const noexcept
654     {
655         Expects(size() > 0);
656         return data()[size() - 1];
657     }
659     constexpr pointer data() const noexcept { return storage_.data(); }
661     // [span.iter], span iterator support
662     constexpr iterator begin() const noexcept
663     {
664         const auto data = storage_.data();
665         // clang-format off
666         GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
667         // clang-format on
668         return {data, data + size(), data};
669     }
671     constexpr iterator end() const noexcept
672     {
673         const auto data = storage_.data();
674         // clang-format off
675         GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
676         // clang-format on
677         const auto endData = data + storage_.size();
678         return {data, endData, endData};
679     }
681     constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
682     constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
684 #ifdef _MSC_VER
685     // Tell MSVC how to unwrap spans in range-based-for
686     constexpr pointer _Unchecked_begin() const noexcept { return data(); }
687     constexpr pointer _Unchecked_end() const noexcept
688     {
689         // clang-format off
690         GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
691         // clang-format on
692         return data() + size();
693     }
694 #endif // _MSC_VER
696 private:
697     // Needed to remove unnecessary null check in subspans
698     struct KnownNotNull
699     {
700         pointer p;
701     };
703     // this implementation detail class lets us take advantage of the
704     // empty base class optimization to pay for only storage of a single
705     // pointer in the case of fixed-size spans
706     template <class ExtentType>
707     class storage_type : public ExtentType
708     {
709     public:
710         // KnownNotNull parameter is needed to remove unnecessary null check
711         // in subspans and constructors from arrays
712         template <class OtherExtentType>
713         constexpr storage_type(KnownNotNull data, OtherExtentType ext)
714             : ExtentType(ext), data_(data.p)
715         {}
717         template <class OtherExtentType>
718         constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
719         {
720             Expects(data || ExtentType::size() == 0);
721         }
723         constexpr pointer data() const noexcept { return data_; }
725     private:
726         pointer data_;
727     };
729     storage_type<details::extent_type<Extent>> storage_;
731     // The rest is needed to remove unnecessary null check
732     // in subspans and constructors from arrays
733     constexpr span(KnownNotNull ptr, size_type count) noexcept : storage_(ptr, count) {}
735     template <std::size_t CallerExtent>
736     class subspan_selector
737     {
738     };
740     template <std::size_t CallerExtent>
741     constexpr span<element_type, dynamic_extent>
742     make_subspan(size_type offset, size_type count, subspan_selector<CallerExtent>) const noexcept
743     {
744         const span<element_type, dynamic_extent> tmp(*this);
745         return tmp.subspan(offset, count);
746     }
748     // clang-format off
749     GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
750     // clang-format on
751     constexpr span<element_type, dynamic_extent>
752     make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
753     {
754         Expects(size() >= offset);
756         if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
758         Expects(size() - offset >= count);
759         return {KnownNotNull{data() + offset}, count};
760     }
763 #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
765 // Deduction Guides
766 template <class Type, std::size_t Extent>
767 span(Type (&)[Extent]) -> span<Type, Extent>;
769 template <class Type, std::size_t Size>
770 span(std::array<Type, Size>&) -> span<Type, Size>;
772 template <class Type, std::size_t Size>
773 span(const std::array<Type, Size>&) -> span<const Type, Size>;
775 template <class Container,
776           class Element = std::remove_pointer_t<decltype(std::declval<Container&>().data())>>
777 span(Container&) -> span<Element>;
779 template <class Container,
780           class Element = std::remove_pointer_t<decltype(std::declval<const Container&>().data())>>
781 span(const Container&) -> span<Element>;
783 #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
785 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
786 #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
787 #pragma clang diagnostic push
788 #pragma clang diagnostic ignored "-Wdeprecated" // Bug in clang-cl.exe which raises a C++17 -Wdeprecated warning about this static constexpr workaround in C++14 mode.
789 #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
790 template <class ElementType, std::size_t Extent>
791 constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
792 #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
793 #pragma clang diagnostic pop
794 #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
795 #endif
797 namespace details
799     // if we only supported compilers with good constexpr support then
800     // this pair of classes could collapse down to a constexpr function
802     // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
803     // constexpr
804     // and so will fail compilation of the template
805     template <class ElementType, std::size_t Extent>
806     struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
807     {
808         static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big.");
809     };
811     template <class ElementType>
812     struct calculate_byte_size<ElementType, dynamic_extent>
813         : std::integral_constant<std::size_t, dynamic_extent>
814     {
815     };
816 } // namespace details
818 // [span.objectrep], views of object representation
819 template <class ElementType, std::size_t Extent>
820 span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
821 as_bytes(span<ElementType, Extent> s) noexcept
823     using type = span<const byte, details::calculate_byte_size<ElementType, Extent>::value>;
825     // clang-format off
826     GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
827     // clang-format on
828     return type{reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
831 template <class ElementType, std::size_t Extent,
832           std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
833 span<byte, details::calculate_byte_size<ElementType, Extent>::value>
834 as_writable_bytes(span<ElementType, Extent> s) noexcept
836     using type = span<byte, details::calculate_byte_size<ElementType, Extent>::value>;
838     // clang-format off
839     GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
840     // clang-format on
841     return type{reinterpret_cast<byte*>(s.data()), s.size_bytes()};
844 } // namespace gsl
846 #if defined(_MSC_VER) && !defined(__clang__)
848 #pragma warning(pop)
849 #endif // _MSC_VER
851 #if defined(__GNUC__) && __GNUC__ > 6
852 #pragma GCC diagnostic pop
853 #endif // __GNUC__ > 6
855 #if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
856 #pragma clang diagnostic pop
857 #endif
859 #endif // GSL_SPAN_H