Debian Codegraph @

src/3rdparty/gsl/span

raw
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.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #ifndef GSL_SPAN_H
18 #define GSL_SPAN_H
19
20 #include "./assert" // for Expects
21 #include "./byte" // for gsl::impl::byte
22 #include "./span_ext" // for span specialization of gsl::at and other span-related extensions
23 #include "./util" // for narrow_cast
24
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...
30
31 #if defined(__has_include) && __has_include(<version>)
32 #include <version>
33 #endif
34
35 #if defined(_MSC_VER) && !defined(__clang__)
36 #pragma warning(push)
37
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
43
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
47
48 #endif // _MSC_VER
49
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))
55
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
65
66 // Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
67 #if defined(__clang__)
68 #if __has_warning("-Wunsafe-buffer-usage")
69 #pragma clang diagnostic push
70 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
71 #endif // __has_warning("-Wunsafe-buffer-usage")
72 #endif // defined(__clang__)
73
74 namespace gsl
75 {
76
77 // implementation details
78 namespace details
79 {
80 template <class T>
81 struct is_span_oracle : std::false_type
82 {
83 };
84
85 template <class ElementType, std::size_t Extent>
86 struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
87 {
88 };
89
90 template <class T>
91 struct is_span : public is_span_oracle<std::remove_cv_t<T>>
92 {
93 };
94
95 template <class T>
96 struct is_std_array_oracle : std::false_type
97 {
98 };
99
100 template <class ElementType, std::size_t Extent>
101 struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
102 {
103 };
104
105 template <class T>
106 struct is_std_array : is_std_array_oracle<std::remove_cv_t<T>>
107 {
108 };
109
110 template <std::size_t From, std::size_t To>
111 struct is_allowed_extent_conversion
112 : std::integral_constant<bool, From == To || To == dynamic_extent>
113 {
114 };
115
116 template <class From, class To>
117 struct is_allowed_element_type_conversion
118 : std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
119 {
120 };
121
122 template <class Type>
123 class span_iterator
124 {
125 public:
126 #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts))
127 using iterator_concept = std::contiguous_iterator_tag;
128 #endif // __cpp_lib_ranges
129 using iterator_category = std::random_access_iterator_tag;
130 using value_type = std::remove_cv_t<Type>;
131 using difference_type = std::ptrdiff_t;
132 using pointer = Type*;
133 using reference = Type&;
134
135 #ifdef _MSC_VER
136 using _Unchecked_type = pointer;
137 using _Prevent_inheriting_unwrap = span_iterator;
138 #endif // _MSC_VER
139 constexpr span_iterator() = default;
140
141 constexpr span_iterator(pointer begin, pointer end, pointer current)
142 : begin_(begin), end_(end), current_(current)
143 {
144 Expects(begin_ <= current_ && current <= end_);
145 }
146
147 constexpr operator span_iterator<const Type>() const noexcept
148 {
149 return {begin_, end_, current_};
150 }
151
152 constexpr reference operator*() const noexcept
153 {
154 Expects(current_ != end_);
155 return *current_;
156 }
157
158 constexpr pointer operator->() const noexcept
159 {
160 Expects(current_ != end_);
161 return current_;
162 }
163 constexpr span_iterator& operator++() noexcept
164 {
165 Expects(current_ != end_);
166 // clang-format off
167 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
168 // clang-format on
169 ++current_;
170 return *this;
171 }
172
173 constexpr span_iterator operator++(int) noexcept
174 {
175 span_iterator ret = *this;
176 ++*this;
177 return ret;
178 }
179
180 constexpr span_iterator& operator--() noexcept
181 {
182 Expects(begin_ != current_);
183 --current_;
184 return *this;
185 }
186
187 constexpr span_iterator operator--(int) noexcept
188 {
189 span_iterator ret = *this;
190 --*this;
191 return ret;
192 }
193
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 }
205
206 constexpr span_iterator operator+(const difference_type n) const noexcept
207 {
208 span_iterator ret = *this;
209 ret += n;
210 return ret;
211 }
212
213 friend constexpr span_iterator operator+(const difference_type n,
214 const span_iterator& rhs) noexcept
215 {
216 return rhs + n;
217 }
218
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 }
228
229 constexpr span_iterator operator-(const difference_type n) const noexcept
230 {
231 span_iterator ret = *this;
232 ret -= n;
233 return ret;
234 }
235
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 }
244
245 constexpr reference operator[](const difference_type n) const noexcept
246 {
247 return *(*this + n);
248 }
249
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 }
258
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 }
266
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 }
275
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 }
283
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 }
291
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 }
299
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 }
310
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 }
317
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 }
327
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
344
345 pointer begin_ = nullptr;
346 pointer end_ = nullptr;
347 pointer current_ = nullptr;
348
349 template <typename Ptr>
350 friend struct std::pointer_traits;
351 };
352 }} // namespace gsl::details
353
354 namespace std
355 {
356 template <class Type>
357 struct pointer_traits<::gsl::details::span_iterator<Type>>
358 {
359 using pointer = ::gsl::details::span_iterator<Type>;
360 using element_type = Type;
361 using difference_type = ptrdiff_t;
362
363 static constexpr element_type* to_address(const pointer i) noexcept { return i.current_; }
364 };
365 } // namespace std
366
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;
373
374 constexpr extent_type() noexcept = default;
375
376 constexpr explicit extent_type(extent_type<dynamic_extent>);
377
378 constexpr explicit extent_type(size_type size) { Expects(size == Ext); }
379
380 constexpr size_type size() const noexcept { return Ext; }
381
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 };
389
390 template <>
391 class extent_type<dynamic_extent>
392 {
393 public:
394 using size_type = std::size_t;
395
396 template <size_type Other>
397 constexpr explicit extent_type(extent_type<Other> ext) : size_(ext.size())
398 {}
399
400 constexpr explicit extent_type(size_type size) : size_(size)
401 {
402 Expects(size != dynamic_extent);
403 }
404
405 constexpr size_type size() const noexcept { return size_; }
406
407 private:
408 size_type size_;
409 };
410
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 }
416
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
425
426 // [span], class template span
427 template <class ElementType, std::size_t Extent>
428 class span
429 {
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;
440
441 using iterator = details::span_iterator<ElementType>;
442 using reverse_iterator = std::reverse_iterator<iterator>;
443
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
449
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 {}
459
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 }
465
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 {}
469
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 }
476
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 {}
481
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 {}
487
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 {}
496
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 {}
505
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 {}
519
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 {}
531
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 {}
544
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 {}
557
558 constexpr span(const span& other) noexcept = default;
559
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 {}
568
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 {}
577
578 ~span() noexcept = default;
579 constexpr span& operator=(const span& other) noexcept = default;
580
581 // [span.sub], span subviews
582 template <std::size_t Count>
583 constexpr span<element_type, Count> first() const noexcept
584 {
585 static_assert(Extent == dynamic_extent || Count <= Extent,
586 "first() cannot extract more elements from a span than it contains.");
587 Expects(Count <= size());
588 return span<element_type, Count>{data(), Count};
589 }
590
591 template <std::size_t Count>
592 // clang-format off
593 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
594 // clang-format on
595 constexpr span<element_type, Count> last() const noexcept
596 {
597 static_assert(Extent == dynamic_extent || Count <= Extent,
598 "last() cannot extract more elements from a span than it contains.");
599 Expects(Count <= size());
600 return span<element_type, Count>{data() + (size() - Count), Count};
601 }
602
603 template <std::size_t Offset, std::size_t Count = dynamic_extent>
604 // clang-format off
605 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
606 // clang-format on
607 constexpr auto subspan() const noexcept ->
608 typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
609 {
610 static_assert(Extent == dynamic_extent || (Extent >= Offset && (Count == dynamic_extent ||
611 Count <= Extent - Offset)),
612 "subspan() cannot extract more elements from a span than it contains.");
613 Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
614 using type =
615 typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type;
616 return type{data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
617 }
618
619 constexpr span<element_type, dynamic_extent> first(size_type count) const noexcept
620 {
621 Expects(count <= size());
622 return {data(), count};
623 }
624
625 constexpr span<element_type, dynamic_extent> last(size_type count) const noexcept
626 {
627 Expects(count <= size());
628 return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
629 }
630
631 constexpr span<element_type, dynamic_extent>
632 subspan(size_type offset, size_type count = dynamic_extent) const noexcept
633 {
634 return make_subspan(offset, count, subspan_selector<Extent>{});
635 }
636
637 // [span.obs], span observers
638 constexpr size_type size() const noexcept { return storage_.size(); }
639
640 constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); }
641
642 constexpr bool empty() const noexcept { return size() == 0; }
643
644 // [span.elem], span element access
645 // clang-format off
646 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
647 // clang-format on
648 constexpr reference operator[](size_type idx) const noexcept
649 {
650 Expects(idx < size());
651 return data()[idx];
652 }
653
654 constexpr reference front() const noexcept
655 {
656 Expects(size() > 0);
657 return data()[0];
658 }
659
660 constexpr reference back() const noexcept
661 {
662 Expects(size() > 0);
663 return data()[size() - 1];
664 }
665
666 constexpr pointer data() const noexcept { return storage_.data(); }
667
668 // [span.iter], span iterator support
669 constexpr iterator begin() const noexcept
670 {
671 const auto data = storage_.data();
672 // clang-format off
673 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
674 // clang-format on
675 return {data, data + size(), data};
676 }
677
678 constexpr iterator end() const noexcept
679 {
680 const auto data = storage_.data();
681 // clang-format off
682 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
683 // clang-format on
684 const auto endData = data + storage_.size();
685 return {data, endData, endData};
686 }
687
688 constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
689 constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
690
691 #ifdef _MSC_VER
692 // Tell MSVC how to unwrap spans in range-based-for
693 constexpr pointer _Unchecked_begin() const noexcept { return data(); }
694 constexpr pointer _Unchecked_end() const noexcept
695 {
696 // clang-format off
697 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
698 // clang-format on
699 return data() + size();
700 }
701 #endif // _MSC_VER
702
703 private:
704 // Needed to remove unnecessary null check in subspans
705 struct KnownNotNull
706 {
707 pointer p;
708 };
709
710 // this implementation detail class lets us take advantage of the
711 // empty base class optimization to pay for only storage of a single
712 // pointer in the case of fixed-size spans
713 template <class ExtentType>
714 class storage_type : public ExtentType
715 {
716 public:
717 // KnownNotNull parameter is needed to remove unnecessary null check
718 // in subspans and constructors from arrays
719 template <class OtherExtentType>
720 constexpr storage_type(KnownNotNull data, OtherExtentType ext)
721 : ExtentType(ext), data_(data.p)
722 {}
723
724 template <class OtherExtentType>
725 constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
726 {
727 Expects(data || ExtentType::size() == 0);
728 }
729
730 constexpr pointer data() const noexcept { return data_; }
731
732 private:
733 pointer data_;
734 };
735
736 storage_type<details::extent_type<Extent>> storage_;
737
738 // The rest is needed to remove unnecessary null check
739 // in subspans and constructors from arrays
740 constexpr span(KnownNotNull ptr, size_type count) noexcept : storage_(ptr, count) {}
741
742 template <std::size_t CallerExtent>
743 class subspan_selector
744 {
745 };
746
747 template <std::size_t CallerExtent>
748 constexpr span<element_type, dynamic_extent>
749 make_subspan(size_type offset, size_type count, subspan_selector<CallerExtent>) const noexcept
750 {
751 const span<element_type, dynamic_extent> tmp(*this);
752 return tmp.subspan(offset, count);
753 }
754
755 // clang-format off
756 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
757 // clang-format on
758 constexpr span<element_type, dynamic_extent>
759 make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
760 {
761 Expects(size() >= offset);
762
763 if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
764
765 Expects(size() - offset >= count);
766 return {KnownNotNull{data() + offset}, count};
767 }
768 };
769
770 #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
771
772 // Deduction Guides
773 template <class Type, std::size_t Extent>
774 span(Type (&)[Extent]) -> span<Type, Extent>;
775
776 template <class Type, std::size_t Size>
777 span(std::array<Type, Size>&) -> span<Type, Size>;
778
779 template <class Type, std::size_t Size>
780 span(const std::array<Type, Size>&) -> span<const Type, Size>;
781
782 template <class Container,
783 class Element = std::remove_pointer_t<decltype(std::declval<Container&>().data())>>
784 span(Container&) -> span<Element>;
785
786 template <class Container,
787 class Element = std::remove_pointer_t<decltype(std::declval<const Container&>().data())>>
788 span(const Container&) -> span<Element>;
789
790 #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
791
792 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
793 #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
794 #pragma clang diagnostic push
795 #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.
796 #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
797 template <class ElementType, std::size_t Extent>
798 constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
799 #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
800 #pragma clang diagnostic pop
801 #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
802 #endif
803
804 namespace details
805 {
806 // if we only supported compilers with good constexpr support then
807 // this pair of classes could collapse down to a constexpr function
808
809 // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
810 // constexpr
811 // and so will fail compilation of the template
812 template <class ElementType, std::size_t Extent>
813 struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
814 {
815 static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big.");
816 };
817
818 template <class ElementType>
819 struct calculate_byte_size<ElementType, dynamic_extent>
820 : std::integral_constant<std::size_t, dynamic_extent>
821 {
822 };
823 } // namespace details
824
825 // [span.objectrep], views of object representation
826 template <class ElementType, std::size_t Extent>
827 span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
828 as_bytes(span<ElementType, Extent> s) noexcept
829 {
830 using type = span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
831
832 // clang-format off
833 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
834 // clang-format on
835 return type{reinterpret_cast<const gsl::impl::byte*>(s.data()), s.size_bytes()};
836 }
837
838 template <class ElementType, std::size_t Extent,
839 std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
840 span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
841 as_writable_bytes(span<ElementType, Extent> s) noexcept
842 {
843 using type = span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
844
845 // clang-format off
846 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
847 // clang-format on
848 return type{reinterpret_cast<gsl::impl::byte*>(s.data()), s.size_bytes()};
849 }
850
851 } // namespace gsl
852
853 #if defined(_MSC_VER) && !defined(__clang__)
854
855 #pragma warning(pop)
856 #endif // _MSC_VER
857
858 #if defined(__GNUC__) && __GNUC__ > 6
859 #pragma GCC diagnostic pop
860 #endif // __GNUC__ > 6
861
862 #if defined(__clang__)
863 #if __has_warning("-Wunsafe-buffer-usage")
864 #pragma clang diagnostic pop
865 #endif // __has_warning("-Wunsafe-buffer-usage")
866 #endif // defined(__clang__)
867
868 #endif // GSL_SPAN_H