Back to home page

EIC code displayed by LXR

 
 

    


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

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