Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/gsl/span is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 ///////////////////////////////////////////////////////////////////////////////
0003 //
0004 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0005 //
0006 // This code is licensed under the MIT License (MIT).
0007 //
0008 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0009 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0010 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0011 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0012 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0013 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0014 // THE SOFTWARE.
0015 //
0016 ///////////////////////////////////////////////////////////////////////////////
0017 
0018 #ifndef GSL_SPAN_H
0019 #define GSL_SPAN_H
0020 
0021 #include "./assert"   // for Expects
0022 #include "./byte"     // for gsl::impl::byte
0023 #include "./span_ext" // for span specialization of gsl::at and other span-related extensions
0024 #include "./util"     // for narrow_cast
0025 
0026 #include <array>       // for array
0027 #include <cstddef>     // for ptrdiff_t, size_t, nullptr_t
0028 #include <iterator>    // for reverse_iterator, distance, random_access_...
0029 #include <memory>      // for pointer_traits
0030 #include <type_traits> // for enable_if_t, declval, is_convertible, inte...
0031 
0032 #if defined(__has_include) && __has_include(<version>)
0033 #include <version>
0034 #endif
0035 
0036 #if defined(_MSC_VER) && !defined(__clang__)
0037 #pragma warning(push)
0038 
0039 // turn off some warnings that are noisy about our Expects statements
0040 #pragma warning(disable : 4127) // conditional expression is constant
0041 #pragma warning(                                                                                   \
0042     disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
0043 #pragma warning(disable : 4702) // unreachable code
0044 
0045 // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
0046 #pragma warning(disable : 26495) // uninitialized member when constructor calls constructor
0047 #pragma warning(disable : 26446) // parser bug does not allow attributes on some templates
0048 
0049 #endif // _MSC_VER
0050 
0051 // See if we have enough C++17 power to use a static constexpr data member
0052 // without needing an out-of-line definition
0053 #if !(defined(__cplusplus) && (__cplusplus >= 201703L))
0054 #define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
0055 #endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
0056 
0057 // GCC 7 does not like the signed unsigned mismatch (size_t ptrdiff_t)
0058 // While there is a conversion from signed to unsigned, it happens at
0059 // compiletime, so the compiler wouldn't have to warn indiscriminately, but
0060 // could check if the source value actually doesn't fit into the target type
0061 // and only warn in those cases.
0062 #if defined(__GNUC__) && __GNUC__ > 6
0063 #pragma GCC diagnostic push
0064 #pragma GCC diagnostic ignored "-Wsign-conversion"
0065 #endif
0066 
0067 // Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
0068 #if defined(__clang__)
0069 #if __has_warning("-Wunsafe-buffer-usage")
0070 #pragma clang diagnostic push
0071 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
0072 #endif // __has_warning("-Wunsafe-buffer-usage")
0073 #endif // defined(__clang__)
0074 
0075 namespace gsl
0076 {
0077 
0078 // implementation details
0079 namespace details
0080 {
0081     template <class T>
0082     struct is_span_oracle : std::false_type
0083     {
0084     };
0085 
0086     template <class ElementType, std::size_t Extent>
0087     struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
0088     {
0089     };
0090 
0091     template <class T>
0092     struct is_span : public is_span_oracle<std::remove_cv_t<T>>
0093     {
0094     };
0095 
0096     template <class T>
0097     struct is_std_array_oracle : std::false_type
0098     {
0099     };
0100 
0101     template <class ElementType, std::size_t Extent>
0102     struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
0103     {
0104     };
0105 
0106     template <class T>
0107     struct is_std_array : is_std_array_oracle<std::remove_cv_t<T>>
0108     {
0109     };
0110 
0111     template <std::size_t From, std::size_t To>
0112     struct is_allowed_extent_conversion
0113         : std::integral_constant<bool, From == To || To == dynamic_extent>
0114     {
0115     };
0116 
0117     template <class From, class To>
0118     struct is_allowed_element_type_conversion
0119         : std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
0120     {
0121     };
0122 
0123     template <class Type>
0124     class span_iterator
0125     {
0126     public:
0127 #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts))
0128         using iterator_concept = std::contiguous_iterator_tag;
0129 #endif // __cpp_lib_ranges
0130         using iterator_category = std::random_access_iterator_tag;
0131         using value_type = std::remove_cv_t<Type>;
0132         using difference_type = std::ptrdiff_t;
0133         using pointer = Type*;
0134         using reference = Type&;
0135 
0136 #ifdef _MSC_VER
0137         using _Unchecked_type = pointer;
0138         using _Prevent_inheriting_unwrap = span_iterator;
0139 #endif // _MSC_VER
0140         constexpr span_iterator() = default;
0141 
0142         constexpr operator span_iterator<const Type>() const noexcept
0143         {
0144             return {begin_, end_, current_};
0145         }
0146 
0147         constexpr reference operator*() const noexcept
0148         {
0149             Expects(current_ != end_);
0150             return *current_;
0151         }
0152 
0153         constexpr pointer operator->() const noexcept
0154         {
0155             Expects(current_ != end_);
0156             return current_;
0157         }
0158         constexpr span_iterator& operator++() noexcept
0159         {
0160             Expects(current_ != end_);
0161             GSL_SUPPRESS(bounds.1)
0162             ++current_;
0163             return *this;
0164         }
0165 
0166         constexpr span_iterator operator++(int) noexcept
0167         {
0168             span_iterator ret = *this;
0169             ++*this;
0170             return ret;
0171         }
0172 
0173         constexpr span_iterator& operator--() noexcept
0174         {
0175             Expects(begin_ != current_);
0176             --current_;
0177             return *this;
0178         }
0179 
0180         constexpr span_iterator operator--(int) noexcept
0181         {
0182             span_iterator ret = *this;
0183             --*this;
0184             return ret;
0185         }
0186 
0187         constexpr span_iterator& operator+=(const difference_type n) noexcept
0188         {
0189             if (n != 0) Expects(begin_ && current_ && end_);
0190             if (n > 0) Expects(end_ - current_ >= n);
0191             if (n < 0) Expects(current_ - begin_ >= -n);
0192             GSL_SUPPRESS(bounds.1)
0193             current_ += n;
0194             return *this;
0195         }
0196 
0197         constexpr span_iterator operator+(const difference_type n) const noexcept
0198         {
0199             span_iterator ret = *this;
0200             ret += n;
0201             return ret;
0202         }
0203 
0204         friend constexpr span_iterator operator+(const difference_type n,
0205                                                  const span_iterator& rhs) noexcept
0206         {
0207             return rhs + n;
0208         }
0209 
0210         constexpr span_iterator& operator-=(const difference_type n) noexcept
0211         {
0212             if (n != 0) Expects(begin_ && current_ && end_);
0213             if (n > 0) Expects(current_ - begin_ >= n);
0214             if (n < 0) Expects(end_ - current_ >= -n);
0215             GSL_SUPPRESS(bounds.1)
0216             current_ -= n;
0217             return *this;
0218         }
0219 
0220         constexpr span_iterator operator-(const difference_type n) const noexcept
0221         {
0222             span_iterator ret = *this;
0223             ret -= n;
0224             return ret;
0225         }
0226 
0227         template <
0228             class Type2,
0229             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0230         constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
0231         {
0232             Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
0233             return current_ - rhs.current_;
0234         }
0235 
0236         constexpr reference operator[](const difference_type n) const noexcept
0237         {
0238             return *(*this + n);
0239         }
0240 
0241         template <
0242             class Type2,
0243             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0244         constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
0245         {
0246             Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
0247             return current_ == rhs.current_;
0248         }
0249 
0250         template <
0251             class Type2,
0252             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0253         constexpr bool operator!=(const span_iterator<Type2>& rhs) const noexcept
0254         {
0255             return !(*this == rhs);
0256         }
0257 
0258         template <
0259             class Type2,
0260             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0261         constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
0262         {
0263             Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
0264             return current_ < rhs.current_;
0265         }
0266 
0267         template <
0268             class Type2,
0269             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0270         constexpr bool operator>(const span_iterator<Type2>& rhs) const noexcept
0271         {
0272             return rhs < *this;
0273         }
0274 
0275         template <
0276             class Type2,
0277             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0278         constexpr bool operator<=(const span_iterator<Type2>& rhs) const noexcept
0279         {
0280             return !(rhs < *this);
0281         }
0282 
0283         template <
0284             class Type2,
0285             std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
0286         constexpr bool operator>=(const span_iterator<Type2>& rhs) const noexcept
0287         {
0288             return !(*this < rhs);
0289         }
0290 
0291 #ifdef _MSC_VER
0292         // MSVC++ iterator debugging support; allows STL algorithms in 15.8+
0293         // to unwrap span_iterator to a pointer type after a range check in STL
0294         // algorithm calls
0295         friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
0296         { // test that [lhs, rhs) forms a valid range inside an STL algorithm
0297             Expects(lhs.begin_ == rhs.begin_ // range spans have to match
0298                     && lhs.end_ == rhs.end_ &&
0299                     lhs.current_ <= rhs.current_); // range must not be transposed
0300         }
0301 
0302         constexpr void _Verify_offset(const difference_type n) const noexcept
0303         { // test that *this + n is within the range of this call
0304             if (n != 0) Expects(begin_ && current_ && end_);
0305             if (n > 0) Expects(end_ - current_ >= n);
0306             if (n < 0) Expects(current_ - begin_ >= -n);
0307         }
0308 
0309         GSL_SUPPRESS(bounds.1)
0310         constexpr pointer _Unwrapped() const noexcept
0311         { // after seeking *this to a high water mark, or using one of the
0312             // _Verify_xxx functions above, unwrap this span_iterator to a raw
0313             // pointer
0314             return current_;
0315         }
0316 
0317         // Tell the STL that span_iterator should not be unwrapped if it can't
0318         // validate in advance, even in release / optimized builds:
0319 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
0320         static constexpr const bool _Unwrap_when_unverified = false;
0321 #else
0322         static constexpr bool _Unwrap_when_unverified = false;
0323 #endif
0324         GSL_SUPPRESS(con.3) // TODO: false positive
0325         constexpr void _Seek_to(const pointer p) noexcept
0326         { // adjust the position of *this to previously verified location p
0327             // after _Unwrapped
0328             current_ = p;
0329         }
0330 #endif
0331 
0332     private:
0333         constexpr span_iterator(pointer begin, pointer end, pointer current)
0334             : begin_(begin), end_(end), current_(current)
0335         {
0336             Expects(begin_ <= current_ && current <= end_);
0337         }
0338 
0339         pointer begin_ = nullptr;
0340         pointer end_ = nullptr;
0341         pointer current_ = nullptr;
0342 
0343         template <class>
0344         friend class span_iterator;
0345         template <class, std::size_t>
0346         friend class ::gsl::span;
0347         template <typename Ptr>
0348         friend struct std::pointer_traits;
0349     };
0350 } // namespace details
0351 } // namespace gsl
0352 
0353 namespace std
0354 {
0355 template <class Type>
0356 struct pointer_traits<::gsl::details::span_iterator<Type>>
0357 {
0358     using pointer = ::gsl::details::span_iterator<Type>;
0359     using element_type = Type;
0360     using difference_type = ptrdiff_t;
0361 
0362     static constexpr element_type* to_address(const pointer i) noexcept { return i.current_; }
0363 };
0364 } // namespace std
0365 
0366 namespace gsl
0367 {
0368 namespace details
0369 {
0370     template <std::size_t Ext>
0371     class extent_type
0372     {
0373     public:
0374         using size_type = std::size_t;
0375 
0376         constexpr extent_type() noexcept = default;
0377 
0378         constexpr explicit extent_type(extent_type<dynamic_extent>);
0379 
0380         constexpr explicit extent_type(size_type size) { Expects(size == Ext); }
0381 
0382         constexpr size_type size() const noexcept { return Ext; }
0383 
0384     private:
0385 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
0386         static constexpr const size_type size_ = Ext; // static size equal to Ext
0387 #else
0388         static constexpr size_type size_ = Ext; // static size equal to Ext
0389 #endif
0390     };
0391 
0392     template <>
0393     class extent_type<dynamic_extent>
0394     {
0395     public:
0396         using size_type = std::size_t;
0397 
0398         template <size_type Other>
0399         constexpr explicit extent_type(extent_type<Other> ext) : size_(ext.size())
0400         {}
0401 
0402         constexpr explicit extent_type(size_type size) : size_(size)
0403         {
0404             Expects(size != dynamic_extent);
0405         }
0406 
0407         constexpr size_type size() const noexcept { return size_; }
0408 
0409     private:
0410         size_type size_;
0411     };
0412 
0413     template <std::size_t Ext>
0414     constexpr extent_type<Ext>::extent_type(extent_type<dynamic_extent> ext)
0415     {
0416         Expects(ext.size() == Ext);
0417     }
0418 
0419     template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
0420     struct calculate_subspan_type
0421     {
0422         using type = span<ElementType, Count != dynamic_extent
0423                                            ? Count
0424                                            : (Extent != dynamic_extent ? Extent - Offset : Extent)>;
0425     };
0426 } // namespace details
0427 
0428 // [span], class template span
0429 template <class ElementType, std::size_t Extent>
0430 class span
0431 {
0432 public:
0433     // constants and types
0434     using element_type = ElementType;
0435     using value_type = std::remove_cv_t<ElementType>;
0436     using size_type = std::size_t;
0437     using pointer = element_type*;
0438     using const_pointer = const element_type*;
0439     using reference = element_type&;
0440     using const_reference = const element_type&;
0441     using difference_type = std::ptrdiff_t;
0442 
0443     using iterator = details::span_iterator<ElementType>;
0444     using reverse_iterator = std::reverse_iterator<iterator>;
0445 
0446 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
0447     static constexpr const size_type extent{Extent};
0448 #else
0449     static constexpr size_type extent{Extent};
0450 #endif
0451 
0452     // [span.cons], span constructors, copy, assignment, and destructor
0453     template <bool Dependent = false,
0454               // "Dependent" is needed to make "std::enable_if_t<Dependent || Extent == 0 || Extent
0455               // == dynamic_extent>" SFINAE, since "std::enable_if_t<Extent == 0 || Extent ==
0456               // dynamic_extent>" is ill-formed when Extent is greater than 0.
0457               class = std::enable_if_t<(Dependent ||
0458                                         details::is_allowed_extent_conversion<0, Extent>::value)>>
0459     constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
0460     {}
0461 
0462     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
0463     constexpr explicit span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
0464     {
0465         Expects(count == Extent);
0466     }
0467 
0468     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
0469     constexpr span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
0470     {}
0471 
0472     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
0473     constexpr explicit span(pointer firstElem, pointer lastElem) noexcept
0474         : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
0475     {
0476         Expects(lastElem - firstElem == static_cast<difference_type>(Extent));
0477     }
0478 
0479     template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
0480     constexpr span(pointer firstElem, pointer lastElem) noexcept
0481         : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
0482     {}
0483 
0484     template <std::size_t N,
0485               std::enable_if_t<details::is_allowed_extent_conversion<N, Extent>::value, int> = 0>
0486     constexpr span(element_type (&arr)[N]) noexcept
0487         : storage_(KnownNotNull{arr}, details::extent_type<N>())
0488     {}
0489 
0490     template <
0491         class T, std::size_t N,
0492         std::enable_if_t<(details::is_allowed_extent_conversion<N, Extent>::value &&
0493                           details::is_allowed_element_type_conversion<T, element_type>::value),
0494                          int> = 0>
0495     constexpr span(std::array<T, N>& arr) noexcept
0496         : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
0497     {}
0498 
0499     template <class T, std::size_t N,
0500               std::enable_if_t<
0501                   (details::is_allowed_extent_conversion<N, Extent>::value &&
0502                    details::is_allowed_element_type_conversion<const T, element_type>::value),
0503                   int> = 0>
0504     constexpr span(const std::array<T, N>& arr) noexcept
0505         : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
0506     {}
0507 
0508     // NB: the SFINAE on these constructors uses .data() as an incomplete/imperfect proxy for the
0509     // requirement on Container to be a contiguous sequence container.
0510     template <std::size_t MyExtent = Extent, class Container,
0511               std::enable_if_t<
0512                   MyExtent != dynamic_extent && !details::is_span<Container>::value &&
0513                       !details::is_std_array<Container>::value &&
0514                       std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
0515                       std::is_convertible<
0516                           std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
0517                           element_type (*)[]>::value,
0518                   int> = 0>
0519     constexpr explicit span(Container& cont) noexcept : span(cont.data(), cont.size())
0520     {}
0521 
0522     template <std::size_t MyExtent = Extent, class Container,
0523               std::enable_if_t<
0524                   MyExtent == dynamic_extent && !details::is_span<Container>::value &&
0525                       !details::is_std_array<Container>::value &&
0526                       std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
0527                       std::is_convertible<
0528                           std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
0529                           element_type (*)[]>::value,
0530                   int> = 0>
0531     constexpr span(Container& cont) noexcept : span(cont.data(), cont.size())
0532     {}
0533 
0534     template <
0535         std::size_t MyExtent = Extent, class Container,
0536         std::enable_if_t<
0537             MyExtent != dynamic_extent && std::is_const<element_type>::value &&
0538                 !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
0539                 std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
0540                 std::is_convertible<
0541                     std::remove_pointer_t<decltype(std::declval<const Container&>().data())> (*)[],
0542                     element_type (*)[]>::value,
0543             int> = 0>
0544     constexpr explicit span(const Container& cont) noexcept : span(cont.data(), cont.size())
0545     {}
0546 
0547     template <
0548         std::size_t MyExtent = Extent, class Container,
0549         std::enable_if_t<
0550             MyExtent == dynamic_extent && std::is_const<element_type>::value &&
0551                 !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
0552                 std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
0553                 std::is_convertible<
0554                     std::remove_pointer_t<decltype(std::declval<const Container&>().data())> (*)[],
0555                     element_type (*)[]>::value,
0556             int> = 0>
0557     constexpr span(const Container& cont) noexcept : span(cont.data(), cont.size())
0558     {}
0559 
0560     constexpr span(const span& other) noexcept = default;
0561 
0562     template <class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
0563               std::enable_if_t<(MyExtent == dynamic_extent || MyExtent == OtherExtent) &&
0564                                    details::is_allowed_element_type_conversion<OtherElementType,
0565                                                                                element_type>::value,
0566                                int> = 0>
0567     constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
0568         : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
0569     {}
0570 
0571     template <class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
0572               std::enable_if_t<MyExtent != dynamic_extent && OtherExtent == dynamic_extent &&
0573                                    details::is_allowed_element_type_conversion<OtherElementType,
0574                                                                                element_type>::value,
0575                                int> = 0>
0576     constexpr explicit span(const span<OtherElementType, OtherExtent>& other) noexcept
0577         : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
0578     {}
0579 
0580     ~span() noexcept = default;
0581     constexpr span& operator=(const span& other) noexcept = default;
0582 
0583     // [span.sub], span subviews
0584     template <std::size_t Count>
0585     constexpr span<element_type, Count> first() const noexcept
0586     {
0587         static_assert(Extent == dynamic_extent || Count <= Extent,
0588                       "first() cannot extract more elements from a span than it contains.");
0589         Expects(Count <= size());
0590         return span<element_type, Count>{data(), Count};
0591     }
0592 
0593     template <std::size_t Count>
0594     GSL_SUPPRESS(bounds.1) constexpr span<element_type, Count> last() const noexcept
0595     {
0596         static_assert(Extent == dynamic_extent || Count <= Extent,
0597                       "last() cannot extract more elements from a span than it contains.");
0598         Expects(Count <= size());
0599         return span<element_type, Count>{data() + (size() - Count), Count};
0600     }
0601 
0602     template <std::size_t Offset, std::size_t Count = dynamic_extent>
0603     GSL_SUPPRESS(bounds.1) constexpr auto subspan() const noexcept ->
0604         typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
0605     {
0606         static_assert(Extent == dynamic_extent || (Extent >= Offset && (Count == dynamic_extent ||
0607                                                                         Count <= Extent - Offset)),
0608                       "subspan() cannot extract more elements from a span than it contains.");
0609         Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
0610         using type =
0611             typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type;
0612         return type{data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
0613     }
0614 
0615     constexpr span<element_type, dynamic_extent> first(size_type count) const noexcept
0616     {
0617         Expects(count <= size());
0618         return {data(), count};
0619     }
0620 
0621     constexpr span<element_type, dynamic_extent> last(size_type count) const noexcept
0622     {
0623         Expects(count <= size());
0624         return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
0625     }
0626 
0627     constexpr span<element_type, dynamic_extent>
0628     subspan(size_type offset, size_type count = dynamic_extent) const noexcept
0629     {
0630         return make_subspan(offset, count, subspan_selector<Extent>{});
0631     }
0632 
0633     // [span.obs], span observers
0634     constexpr size_type size() const noexcept { return storage_.size(); }
0635 
0636     constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); }
0637 
0638     constexpr bool empty() const noexcept { return size() == 0; }
0639 
0640     // [span.elem], span element access
0641     GSL_SUPPRESS(bounds.1)
0642     constexpr reference operator[](size_type idx) const noexcept
0643     {
0644         Expects(idx < size());
0645         return data()[idx];
0646     }
0647 
0648     constexpr reference front() const noexcept
0649     {
0650         Expects(size() > 0);
0651         return data()[0];
0652     }
0653 
0654     constexpr reference back() const noexcept
0655     {
0656         Expects(size() > 0);
0657         return data()[size() - 1];
0658     }
0659 
0660     constexpr pointer data() const noexcept { return storage_.data(); }
0661 
0662     // [span.iter], span iterator support
0663     constexpr iterator begin() const noexcept
0664     {
0665         const auto data = storage_.data();
0666         GSL_SUPPRESS(bounds.1)
0667         return {data, data + size(), data};
0668     }
0669 
0670     constexpr iterator end() const noexcept
0671     {
0672         const auto data = storage_.data();
0673         GSL_SUPPRESS(bounds.1)
0674         const auto endData = data + storage_.size();
0675         return {data, endData, endData};
0676     }
0677 
0678     constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
0679     constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
0680 
0681 #ifdef _MSC_VER
0682     // Tell MSVC how to unwrap spans in range-based-for
0683     constexpr pointer _Unchecked_begin() const noexcept { return data(); }
0684     constexpr pointer _Unchecked_end() const noexcept
0685     {
0686         GSL_SUPPRESS(bounds.1)
0687         return data() + size();
0688     }
0689 #endif // _MSC_VER
0690 
0691 private:
0692     // Needed to remove unnecessary null check in subspans
0693     struct KnownNotNull
0694     {
0695         pointer p;
0696     };
0697 
0698     // this implementation detail class lets us take advantage of the
0699     // empty base class optimization to pay for only storage of a single
0700     // pointer in the case of fixed-size spans
0701     template <class ExtentType>
0702     class storage_type : public ExtentType
0703     {
0704     public:
0705         // KnownNotNull parameter is needed to remove unnecessary null check
0706         // in subspans and constructors from arrays
0707         template <class OtherExtentType>
0708         constexpr storage_type(KnownNotNull data, OtherExtentType ext)
0709             : ExtentType(ext), data_(data.p)
0710         {}
0711 
0712         template <class OtherExtentType>
0713         constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
0714         {
0715             Expects(data || ExtentType::size() == 0);
0716         }
0717 
0718         constexpr pointer data() const noexcept { return data_; }
0719 
0720     private:
0721         pointer data_;
0722     };
0723 
0724     storage_type<details::extent_type<Extent>> storage_;
0725 
0726     // The rest is needed to remove unnecessary null check
0727     // in subspans and constructors from arrays
0728     constexpr span(KnownNotNull ptr, size_type count) noexcept : storage_(ptr, count) {}
0729 
0730     template <std::size_t CallerExtent>
0731     class subspan_selector
0732     {
0733     };
0734 
0735     template <std::size_t CallerExtent>
0736     constexpr span<element_type, dynamic_extent>
0737     make_subspan(size_type offset, size_type count, subspan_selector<CallerExtent>) const noexcept
0738     {
0739         const span<element_type, dynamic_extent> tmp(*this);
0740         return tmp.subspan(offset, count);
0741     }
0742 
0743     GSL_SUPPRESS(bounds.1)
0744     constexpr span<element_type, dynamic_extent>
0745     make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
0746     {
0747         Expects(size() >= offset);
0748 
0749         if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
0750 
0751         Expects(size() - offset >= count);
0752         return {KnownNotNull{data() + offset}, count};
0753     }
0754 };
0755 
0756 #if defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201703L)
0757 
0758 // Deduction Guides
0759 template <class Type, std::size_t Extent>
0760 span(Type (&)[Extent]) -> span<Type, Extent>;
0761 
0762 template <class Type, std::size_t Size>
0763 span(std::array<Type, Size>&) -> span<Type, Size>;
0764 
0765 template <class Type, std::size_t Size>
0766 span(const std::array<Type, Size>&) -> span<const Type, Size>;
0767 
0768 template <class Container,
0769           class Element = std::remove_pointer_t<decltype(std::declval<Container&>().data())>>
0770 span(Container&) -> span<Element>;
0771 
0772 template <class Container,
0773           class Element = std::remove_pointer_t<decltype(std::declval<const Container&>().data())>>
0774 span(const Container&) -> span<Element>;
0775 
0776 #endif // defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201703L)
0777 
0778 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
0779 #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
0780 #pragma clang diagnostic push
0781 #pragma clang diagnostic ignored                                                                   \
0782     "-Wdeprecated" // Bug in clang-cl.exe which raises a C++17 -Wdeprecated warning about this
0783                    // static constexpr workaround in C++14 mode.
0784 #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
0785 template <class ElementType, std::size_t Extent>
0786 constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
0787 #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
0788 #pragma clang diagnostic pop
0789 #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
0790 #endif
0791 
0792 namespace details
0793 {
0794     // if we only supported compilers with good constexpr support then
0795     // this pair of classes could collapse down to a constexpr function
0796 
0797     // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
0798     // constexpr
0799     // and so will fail compilation of the template
0800     template <class ElementType, std::size_t Extent>
0801     struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
0802     {
0803         static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big.");
0804     };
0805 
0806     template <class ElementType>
0807     struct calculate_byte_size<ElementType, dynamic_extent>
0808         : std::integral_constant<std::size_t, dynamic_extent>
0809     {
0810     };
0811 } // namespace details
0812 
0813 // [span.objectrep], views of object representation
0814 template <class ElementType, std::size_t Extent>
0815 span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
0816 as_bytes(span<ElementType, Extent> s) noexcept
0817 {
0818     using type =
0819         span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
0820 
0821     GSL_SUPPRESS(type.1)
0822     return type{reinterpret_cast<const gsl::impl::byte*>(s.data()), s.size_bytes()};
0823 }
0824 
0825 template <class ElementType, std::size_t Extent,
0826           std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
0827 span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
0828 as_writable_bytes(span<ElementType, Extent> s) noexcept
0829 {
0830     using type = span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
0831 
0832     GSL_SUPPRESS(type.1)
0833     return type{reinterpret_cast<gsl::impl::byte*>(s.data()), s.size_bytes()};
0834 }
0835 
0836 } // namespace gsl
0837 
0838 #if defined(_MSC_VER) && !defined(__clang__)
0839 
0840 #pragma warning(pop)
0841 #endif // _MSC_VER
0842 
0843 #if defined(__GNUC__) && __GNUC__ > 6
0844 #pragma GCC diagnostic pop
0845 #endif // __GNUC__ > 6
0846 
0847 #if defined(__clang__)
0848 #if __has_warning("-Wunsafe-buffer-usage")
0849 #pragma clang diagnostic pop
0850 #endif // __has_warning("-Wunsafe-buffer-usage")
0851 #endif // defined(__clang__)
0852 
0853 #endif // GSL_SPAN_H