BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_span.h
Go to the documentation of this file.
1/// @file bslstl_span.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_span.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_SPAN
9#define INCLUDED_BSLSTL_SPAN
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_span bslstl_span
15/// @brief Provide a (mostly) standard-compliant `span` class template.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_span
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_span-purpose"> Purpose</a>
25/// * <a href="#bslstl_span-classes"> Classes </a>
26/// * <a href="#bslstl_span-description"> Description </a>
27/// * <a href="#bslstl_span-usage"> Usage </a>
28/// * <a href="#bslstl_span-example-1-using-span-to-pass-a-portion-of-an-array-as-a-container"> Example 1: Using Span To Pass A Portion Of An Array As A Container </a>
29/// * <a href="#bslstl_span-example-2-returning-a-subset-of-a-container-from-a-function"> Example 2: Returning A Subset Of A Container From A Function </a>
30///
31/// # Purpose {#bslstl_span-purpose}
32/// Provide a (mostly) standard-compliant `span` class template.
33///
34/// # Classes {#bslstl_span-classes}
35///
36/// - bsl::span: C++03-compliant implementation of `std::span`.
37///
38/// **Canonical header:** bsl_span.h
39///
40/// @see ISO C++ Standard
41///
42/// # Description {#bslstl_span-description}
43/// This component provides the C+20 standard view type `span`,
44/// that is a view over a contiguous sequence of objects. Note that if compiler
45/// supports the C++20 standard, then the `std` implementation of `span` is
46/// used.
47///
48/// There are two implementations of `span`; one for `statically sized` (i. e.,
49/// size fixed at compile time) spans, and `dynamically sized` (size can be
50/// altered at run-time).
51///
52/// `bsl::span` differs from `std::span` in the following ways:
53/// * The `constexpr` inline symbol `std::dynamic_extent` has been replaced by
54/// an enumeration for C++03 compatibility.
55/// * A `bsl::span` can be implicitly constructed from a `bsl::array`.
56/// * The implicit construction from an arbitrary container that supports
57/// `data()` and `size()` is enabled only for C++11 and later.
58/// * bsl::span is implicitly constructible from a bsl::vector in C++03.
59///
60/// ## Usage {#bslstl_span-usage}
61///
62///
63/// This section illustrates intended usage of this component.
64///
65/// ### Example 1: Using Span To Pass A Portion Of An Array As A Container {#bslstl_span-example-1-using-span-to-pass-a-portion-of-an-array-as-a-container}
66///
67///
68/// Suppose we already have an array of values of type `TYPE`, and we want to
69/// pass a subset of the array to a function, which is expecting some kind of a
70/// container. We can create a `span` from the array, and then pass that.
71/// Since the span is a `view` into the array, i.e, the span owns no storage,
72/// the elements in the span are the same as the ones in the array.
73///
74/// First, we create a template function that takes a generic container. This
75/// function inspects each of the (numeric) values in the container, and if the
76/// low bit is set, flips it. This has the effect of turning odd values into
77/// even values.
78/// @code
79/// template <class CONTAINER>
80/// void MakeEven(CONTAINER &c)
81/// // Make every value in the specified container 'c' even.
82/// {
83/// for (typename CONTAINER::iterator it = c.begin();
84/// it != c.end();
85/// ++it) {
86/// if (*it & 1) {
87/// *it ^= 1;
88/// }
89/// }
90/// }
91/// @endcode
92/// We then create a span, and verify that it contains the values that we
93/// expect, and pass it to `MakeEven` to modify it. Afterwards, we check that
94/// none of the elements in the array that were not included in the span are
95/// unchanged, and the ones in the span were.
96/// @code
97/// int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
98/// bsl::span<int> sp(arr + 3, 4); // 4 elements, starting at 3.
99/// for (int i = 0; i < 10; ++i)
100/// {
101/// assert(arr[i] == i);
102/// }
103///
104/// assert(sp[0] == 3);
105/// assert(sp[1] == 4);
106/// assert(sp[2] == 5);
107/// assert(sp[3] == 6);
108///
109/// MakeEven(sp);
110///
111/// assert(sp[0] == 2); // Has been changed
112/// assert(sp[1] == 4);
113/// assert(sp[2] == 4); // Has been changed
114/// assert(sp[3] == 6);
115///
116/// assert(arr[0] == 0); // Not part of the span
117/// assert(arr[1] == 1); // Not part of the span
118/// assert(arr[2] == 2); // Not part of the span
119/// assert(arr[3] == 2); // Has been changed
120/// assert(arr[4] == 4);
121/// assert(arr[5] == 4); // Has been changed
122/// assert(arr[6] == 6);
123/// assert(arr[7] == 7); // Not part of the span
124/// assert(arr[8] == 8); // Not part of the span
125/// assert(arr[9] == 9); // Not part of the span
126/// @endcode
127///
128/// ### Example 2: Returning A Subset Of A Container From A Function {#bslstl_span-example-2-returning-a-subset-of-a-container-from-a-function}
129///
130///
131/// Suppose we already have a vector of values of type `TYPE`, and we want to
132/// return a (contiguous) subset of the vector from a function, which can then
133/// be processed processed using a range-based for loop. To achieve that, we
134/// can use `span` as the return type. The calling code can then interate over
135/// the span as if it was a container. Note that since the span doesn't own the
136/// elements of the vector, the span might become invalid when the vector is
137/// changed (or resized, or destroyed).
138///
139/// First, we create the vector and define our function that returns a slice as
140/// a `span`.
141/// @code
142/// bsl::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
143///
144/// /// Return a span into the specified `vec`, starting at the specified
145/// /// `first` index, and continuing up to (but not including) the
146/// /// specified `last` index.
147/// bsl::span<const int> slice(const bsl::vector<int>& vec,
148/// size_t first,
149/// size_t last)
150/// {
151/// return bsl::span<const int>(vec.data() + first, last-first);
152/// }
153/// @endcode
154/// We can now iterate over the elements in the slice using the span:
155/// @code
156/// bsl::span<const int> sp = slice(v, 4, 7);
157/// int val = 4;
158/// for (int x: sp) {
159/// assert(x == val++);
160/// }
161/// @endcode
162/// Note that we can use the return value directly and avoid declaring the
163/// variable `sp`:
164/// @code
165/// val = 2;
166/// for (int x: slice(v, 2, 8)) {
167/// assert(x == val++);
168/// }
169/// @endcode
170/// @}
171/** @} */
172/** @} */
173
174/** @addtogroup bsl
175 * @{
176 */
177/** @addtogroup bslstl
178 * @{
179 */
180/** @addtogroup bslstl_span
181 * @{
182 */
183
184#include <bslscm_version.h>
185
187#include <bsls_libraryfeatures.h>
188
189#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
190#include <span>
191namespace bsl {
192 using std::dynamic_extent;
193 using std::span;
194 using std::as_bytes;
195 using std::as_writable_bytes;
196}
197#else
198
199#include <bslmf_assert.h>
200#include <bslmf_enableif.h>
202#include <bslmf_isconvertible.h>
203#include <bslmf_removepointer.h>
204#include <bslmf_removecv.h>
205
206#include <bsls_assert.h>
207#include <bsls_keyword.h>
208#include <bsls_nullptr.h>
209
210#include <bslstl_array.h>
211#include <bslstl_iterator.h>
212#include <bslstl_string.h>
213#include <bslstl_vector.h>
214
215#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
216#include <array> // 'std::array'
217#endif
218#include <iterator> // 'reverse_iterator', '!='
219#include <stddef.h> // 'size_t', 'NULL', 'std::byte'
220
221namespace bsl {
222
223enum { dynamic_extent = size_t(-1) };
224
225template <class TYPE, size_t EXTENT = dynamic_extent> class span;
226
227
228 // ===================
229 // struct Span_Utility
230 // ===================
231
232/// This component-private struct provides a namespace for meta-programming
233/// utilities used by the `span` implementation.
235{
236 // PUBLIC TYPES
237
238 /// A metaclass that derives from `true_type` if an array of type `FROM`
239 /// objects (or functions) can be implicitly converted to an array of
240 /// type `TO` objects (or functions). Typically, this is a test that
241 /// `TO` is the same type as `FROM`, but potentially having a stricter
242 /// cv-qualification. Note that the preferred implementation would use
243 /// arrays of unknown bound to be clear that there is nothing specific
244 /// about the length of the arrays; however, to avoid warnings on the
245 /// Solaris compiler that complains about the use of pointers to arrays
246 /// of unknown bound, we arbitrarily pick the array length of 5, as the
247 /// behavior is the same regardless of the length of the arrays.
248 template <class FROM, class TO>
249 struct IsArrayConvertible : bsl::is_convertible<FROM(*)[5], TO(*)[5]>::type
250 {
251 };
252
253 template <class TYPE, size_t EXTENT, size_t COUNT, size_t OFFSET>
255 {
256 // PUBLIC TYPES
257 typedef bsl::span<TYPE, COUNT != dynamic_extent
258 ? COUNT
259 : EXTENT - OFFSET> type;
260
261 };
262
263 template <class TYPE>
265 {
266 // PUBLIC TYPES
267 typedef TYPE type;
268 };
269
270#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
271 template <class TP>
272 struct IsSpanImpl : public bsl::false_type {};
273
274 template <class TP, size_t SZ>
275 struct IsSpanImpl<span<TP, SZ> > : public bsl::true_type {};
276
277 template <class TP>
278 struct IsSpan : public IsSpanImpl<typename bsl::remove_cv<TP>::type> {};
279
280 template <class TP>
281 struct IsBSLArrayImpl : public bsl::false_type {};
282
283 template <class TP, size_t SZ>
284 struct IsBSLArrayImpl<bsl::array<TP, SZ> > : public bsl::true_type {};
285
286 template <class TP>
288 : public IsBSLArrayImpl<typename bsl::remove_cv<TP>::type> {};
289
290 template <class TP>
291 struct IsSTDArrayImpl : public bsl::false_type {};
292
293 template <class TP, size_t SZ>
294 struct IsSTDArrayImpl<std::array<TP, SZ> > : public bsl::true_type {};
295
296 template <class TP>
298 : public IsSTDArrayImpl<typename bsl::remove_cv<TP>::type> {};
299
300 template <class TP, class ELEMENT_TYPE, class = void>
302
303 template <class TP, class ELEMENT_TYPE>
304 struct IsSpanCompatibleContainer<TP, ELEMENT_TYPE,
305 bsl::void_t<
306 // is not a specialization of span
307 typename bsl::enable_if<!IsSpan<TP>::value, bsl::nullptr_t>::type,
308 // is not a specialization of bsl::array
309 typename bsl::enable_if<
310 !IsBSLArray<TP>::value, bsl::nullptr_t>::type,
311 // is not a specialization of std::array
312 typename bsl::enable_if<
313 !IsSTDArray<TP>::value, bsl::nullptr_t>::type,
314 // is not a C-style array
315 typename bsl::enable_if<
316 !bsl::is_array<TP>::value, bsl::nullptr_t>::type,
317 // data(cont) and size(cont) are well formed
318 decltype(bsl::data(std::declval<TP>())),
319 decltype(bsl::size(std::declval<TP>())),
320 // The underlying types are compatible
321 typename bsl::enable_if<
322 Span_Utility::IsArrayConvertible<
323 typename bsl::remove_pointer<
324 decltype(bsl::data(std::declval<TP &>()))>::type,
325 ELEMENT_TYPE>::value,
326 bsl::nullptr_t>::type
327 > >
328 : public bsl::true_type {};
329#endif
330
331};
332
333template <class TYPE, size_t EXTENT>
334class span {
335
336 public:
337 // PUBLIC TYPES
338 typedef TYPE element_type;
340 typedef size_t size_type;
341 typedef ptrdiff_t difference_type;
342 typedef TYPE *pointer;
343 typedef const TYPE *const_pointer;
344 typedef TYPE& reference;
345 typedef const TYPE& const_reference;
347 typedef bsl::reverse_iterator<iterator> reverse_iterator;
348
349// BDE_VERIFY pragma: push
350// BDE_VERIFY pragma: -MN03 // Constant ... names must begin with 's_' or 'k_'
351 // PUBLIC CLASS DATA
352 static const size_type extent = EXTENT;
353// BDE_VERIFY pragma: pop
354
355 // CREATORS
356
357 /// Construct an empty `span` object. The behavior is undefined unless
358 /// `0 == EXTENT`
360
361 /// Create a span that refers to the same data as the specified
362 /// `original` object.
363 BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span& original)
365
366 /// Construct a span that refers to the specified `count` consecutive
367 /// objects starting from the specified `ptr`. The behavior is
368 /// undefined unless `EXTENT == count`.
370
371 /// Construct a span from the specified `first` and specified `last`.
372 /// The behavior is undefined unless
373 /// `EXTENT == bsl::distance(first, last)`.
375
376 /// Construct a span from the specified C-style array `arr`. The
377 /// behavior is undefined unless `SIZE == EXTENT`.
378 template <size_t SIZE>
380 typename Span_Utility::TypeIdentity<element_type>::type (&arr)[SIZE])
382
383#ifndef BSLSTL_ARRAY_IS_ALIASED
384 /// Construct a span from the specified bsl::array `arr`. This
385 /// constructor participates in overload resolution only if
386 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
387 template <class t_OTHER_TYPE>
390 typename bsl::enable_if<
392 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
393
394 /// Construct a span from the specified bsl::array `arr`. This
395 /// constructor participates in overload resolution only if
396 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
397 template <class t_OTHER_TYPE>
400 typename bsl::enable_if<
402 const t_OTHER_TYPE, element_type>::value,
403 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
404#endif
405
406#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
407 /// Construct a span from the specified std::array `arr`. This
408 /// constructor participates in overload resolution only if
409 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
410 template <class t_OTHER_TYPE>
412 std::array<t_OTHER_TYPE, EXTENT>& arr,
413 typename bsl::enable_if<
415 t_OTHER_TYPE, element_type>::value,
416 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
417
418 /// Construct a span from the specified std::array `arr`. This
419 /// constructor participates in overload resolution only if
420 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
421 template <class t_OTHER_TYPE>
423 const std::array<t_OTHER_TYPE, EXTENT>& arr,
424 typename bsl::enable_if<
426 const t_OTHER_TYPE, element_type>::value,
427 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
428#endif
429
430 /// Construct a span from the specified span `other`. This constructor
431 /// participates in overload resolution only if `t_OTHER_TYPE(*)[]` is
432 /// convertible to `element_type(*)[]`.
433 template <class t_OTHER_TYPE>
435 const span<t_OTHER_TYPE, EXTENT>& other,
436 typename bsl::enable_if<
438 t_OTHER_TYPE, element_type>::value,
439 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
440
441 /// Construct a span from the specified span `other`. This constructor
442 /// participates in overload resolution only if `t_OTHER_TYPE(*)[]` is
443 /// convertible to `element_type(*)[]`. The behavior is undefined
444 /// unless `other.size() == EXTENT`.
445 template <class t_OTHER_TYPE>
448 typename bsl::enable_if<
450 t_OTHER_TYPE, element_type>::value,
451 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
452
453 /// Destroy this object.
454 ~span() noexcept = default;
455
456 // ACCESSORS
457
458 /// Return a reference to the last element of this span. The behavior
459 /// is undefined if this span is empty.
461 {
462 // Implemented inline because of Sun/AIX compiler limitations.
463 BSLMF_ASSERT(EXTENT > 0);
464 return d_data_p[size() - 1];
465 }
466
467 /// Return a pointer to the data referenced by this span.
469
470 /// Return `true` if this span contains no elements and `false`
471 /// otherwise.
473
474 /// Return a statically-sized span consisting of the first `COUNT`
475 /// elements of this span. The behavior is undefined unless
476 /// `COUNT <= size()`.
477 template <size_t COUNT>
479 span<element_type, COUNT> first() const;
480
481 /// Return a dynamically-sized span consisting of the first (specified)
482 /// `count` elements of this span. The behavior is undefined unless
483 /// `count <= size()`.
486 first(size_type count) const;
487
488 /// Return a reference to the first element of this span. The behavior
489 /// is undefined if this span is empty.
491 {
492 // Implemented inline because of Sun/AIX compiler limitations.
493 BSLMF_ASSERT(EXTENT > 0);
494 return d_data_p[0];
495 }
496
497 /// Return a statically-sized span consisting of the last `COUNT`
498 /// elements of this span. The behavior is undefined unless
499 /// `COUNT <= size()`.
500 template <size_t COUNT>
503
504 /// Return a dynamically-sized span consisting of the last (specified)
505 /// `count` elements of this span. The behavior is undefined unless
506 /// `count <= size()`.
509 last(size_type count) const;
510
511 /// Return the size of this span.
513 {
514 // Implemented inline because of Sun/AIX compiler limitations.
515 return EXTENT;
516 }
517
518 /// Return the size of this span in bytes.
520
521 /// If the template parameter `COUNT` is @ref dynamic_extent , return a
522 /// dynamically-sized span consisting consisting of the elements of this
523 /// span in the half-open range `[OFFSET, EXTENT)`. Otherwise, return a
524 /// statically-sized span consisting of the elements of this span in the
525 /// half-open range `[OFFSET, OFFSET+COUNT)`. The behavior is undefined
526 /// unless `OFFSET <= EXTENT`. If `COUNT != dynamic_extent`, the
527 /// behavior is undefined unless `OFFSET + COUNT <= EXTENT`.
528 template <size_t OFFSET,
529#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULT_TEMPLATE_ARGS
530 size_t COUNT = dynamic_extent>
531#else
532 size_t COUNT>
533#endif
536 subspan() const
537 {
538 // Implemented inline because of Sun/AIX compiler limitations.
539 typedef typename
541 ReturnType;
542 BSLMF_ASSERT(OFFSET <= EXTENT);
543 BSLMF_ASSERT(COUNT == dynamic_extent || OFFSET + COUNT <= EXTENT);
544 return ReturnType(data() + OFFSET,
545 COUNT == dynamic_extent ? size() - OFFSET : COUNT);
546 }
547
548 /// Return a dynamically-sized span starting at the specified `offset`.
549 /// If the optionally specified `count` is @ref dynamic_extent , the span
550 /// will consist of the half-open range `[offset, size () - offset)` and
551 /// the behavior is undefined if `offset > size()`. Otherwise, the span
552 /// will consist of the half-open range `[offset, count)` and the
553 /// behavior is undefined if `offset + count > size()`.
556 subspan(size_type offset, size_type count = dynamic_extent) const;
557
558 /// Return a reference to the element at the specified `index`. The
559 /// behavior is undefined unless `index < size()`.
562 {
563 // Implemented inline because of Sun/AIX compiler limitations.
564 BSLS_ASSERT(index < size());
565 return d_data_p[index];
566 }
567
568 // ITERATOR OPERATIONS
569
570
571 /// Return an iterator providing modifiable access to the first element
572 /// of this span, and the past-the-end iterator if this span is empty.
574
575 /// Return the past-the-end iterator providing modifiable access to this
576 /// span.
579
580 /// Return a reverse iterator providing modifiable access to the last
581 /// element of this span, and the past-the-end reverse iterator if this
582 /// span is empty.
585
586 /// Return the past-the-end reverse iterator providing modifiable access
587 /// to this span.
590
591 // MANIPULATORS
592
593 /// Assign to this span the value of the specified `rhs` object, and
594 /// return a reference providing modifiable access to this span.
596 span& operator=(const span&) BSLS_KEYWORD_NOEXCEPT;
597
598 /// Exchange the value of this span with the value of the specified
599 /// `other` object.
601
602 private:
603 // DATA
604 pointer d_data_p;
605};
606
607
608template <class TYPE>
609class span<TYPE, dynamic_extent> {
610 public:
611 // PUBLIC TYPES
612 typedef TYPE element_type;
614 typedef size_t size_type;
615 typedef ptrdiff_t difference_type;
616 typedef TYPE *pointer;
617 typedef const TYPE *const_pointer;
618 typedef TYPE& reference;
619 typedef const TYPE& const_reference;
621 typedef bsl::reverse_iterator<iterator> reverse_iterator;
622
623// BDE_VERIFY pragma: push
624// BDE_VERIFY pragma: -MN03 // Constant ... names must begin with 's_' or 'k_'
625 // PUBLIC CLASS DATA
627// BDE_VERIFY pragma: pop
628
629 // CREATORS
630
631 /// Construct an empty `span` object.
633
634 /// Create a span that refers to the same data as the specified
635 /// `original` object.
638
639 /// Construct a span that refers to the specified `count` consecutive
640 /// objects starting from the specified `ptr`.
642
643 /// Construct a span from the specified `first` and specified `last`.
645
646 /// Construct a span from the specified C-style array `arr`.
647 template <size_t SIZE>
649 typename Span_Utility::TypeIdentity<element_type>::type (&arr)[SIZE])
651
652#ifndef BSLSTL_ARRAY_IS_ALIASED
653 /// Construct a span from the specified bsl::array `arr`. This
654 /// constructor participates in overload resolution only if
655 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
656 template <class t_OTHER_TYPE, size_t SIZE>
658 typename bsl::enable_if<
660 t_OTHER_TYPE, element_type>::value,
661 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
662
663 /// Construct a span from the specified bsl::array `arr`. This
664 /// constructor participates in overload resolution only if
665 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
666 template <class t_OTHER_TYPE, size_t SIZE>
669 typename bsl::enable_if<
671 const t_OTHER_TYPE, element_type>::value,
672 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
673#endif
674
675#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
676 /// Construct a span from the specified std::array `arr`. This
677 /// constructor participates in overload resolution only if
678 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
679 template <class t_OTHER_TYPE, size_t SIZE>
680 BSLS_KEYWORD_CONSTEXPR_CPP14 span(std::array<t_OTHER_TYPE, SIZE>& arr,
681 typename bsl::enable_if<
683 t_OTHER_TYPE, element_type>::value,
684 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
685
686 /// Construct a span from the specified std::array `arr`. This
687 /// constructor participates in overload resolution only if
688 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
689 template <class t_OTHER_TYPE, size_t SIZE>
691 const std::array<t_OTHER_TYPE, SIZE>& arr,
692 typename bsl::enable_if<
694 const t_OTHER_TYPE, element_type>::value,
695 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
696#endif
697
698#ifndef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
699 /// Construct a span from the specified bsl::vector `v`. This
700 /// constructor participates in overload resolution only if
701 /// `t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
702 template <class t_OTHER_TYPE, class ALLOCATOR>
704 typename bsl::enable_if<
706 t_OTHER_TYPE, element_type>::value,
707 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
708
709 /// Construct a span from the specified bsl::vector `v`. This
710 /// constructor participates in overload resolution only if
711 /// `const t_OTHER_TYPE(*)[]` is convertible to `element_type(*)[]`.
712 template <class t_OTHER_TYPE, class ALLOCATOR>
714 typename bsl::enable_if<
716 const t_OTHER_TYPE, element_type>::value,
717 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
718
719 /// Construct a span from the specified bsl::string `s`. This
720 /// constructor participates in overload resolution only if
721 /// `CHAR_TYPE(*)[]` is convertible to `element_type(*)[]`.
722 template<class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
724 typename bsl::enable_if<
726 CHAR_TYPE, element_type>::value,
727 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
728
729 /// Construct a span from the specified bsl::string `s`. This
730 /// constructor participates in overload resolution only if
731 /// `const CHAR_TYPE(*)[]` is convertible to `element_type(*)[]`.
732 template<class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
734 typename bsl::enable_if<
736 const CHAR_TYPE, element_type>::value,
737 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
738
739 /// Construct a span from the specified bsl::string_view `sv`. This
740 /// constructor participates in overload resolution only if
741 /// `const CHAR_TYPE(*)[]` is convertible to `element_type(*)[]`.
742 template<class CHAR_TYPE, class CHAR_TRAITS>
744 typename bsl::enable_if<
746 const CHAR_TYPE, element_type>::value,
747 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
748#endif
749
750#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
751 template <class CONTAINER>
753 CONTAINER& c,
754 typename bsl::enable_if<
756 void *>::type = NULL)
757 : d_data_p(bsl::data(c))
758 , d_size(bsl::size(c))
759 {
760 }
761
762 template <class CONTAINER>
764 const CONTAINER& c,
765 typename bsl::enable_if<
767 void *>::type = NULL)
768 : d_data_p(bsl::data(c))
769 , d_size(bsl::size(c))
770 {
771 }
772#endif
773
774 /// Construct a span from the specified span `other`. This constructor
775 /// participates in overload resolution only if `t_OTHER_TYPE(*)[]` is
776 /// convertible to `element_type(*)[]`.
777 template <class t_OTHER_TYPE, size_t OTHER_EXTENT>
780 typename bsl::enable_if<
782 void *>::type = NULL) BSLS_KEYWORD_NOEXCEPT;
783
784 /// Destroy this object.
785 ~span() noexcept = default;
786
787 // ACCESSORS
788
789 /// Return a reference to the last element of this span. The behavior
790 /// is undefined if this span is empty.
792
793 /// Return a pointer to the data referenced by this span.
795
796 // Return `true` if `size() == 0` and `false` otherwise.
798
799 /// Return a statically-sized span consisting of the first `COUNT`
800 /// elements of this span. The behavior is undefined unless
801 /// `COUNT <= size()`.
802 template <size_t COUNT>
804 span<element_type, COUNT> first() const;
805
806 /// Return a dynamically-sized span consisting of the first (specified)
807 /// `count` elements of this span. The behavior is undefined unless
808 /// `count <= size()`.
811 first(size_type count) const;
812
813 /// Return a reference to the first element of this span. The behavior
814 /// is undefined if this span is empty.
816
817 /// Return a statically-sized span consisting of the last `COUNT`
818 /// elements of this span. The behavior is undefined unless
819 /// `COUNT <= size()`.
820 template <size_t COUNT>
822 span<element_type, COUNT> last() const;
823
824 /// Return a dynamically-sized span consisting of the last (specified)
825 /// `count` elements of this span. The behavior is undefined unless
826 /// `count <= size()`.
829 last(size_type count) const;
830
831 /// Return the size of this span.
833
834 /// Return the size of this span in bytes.
836
837 /// Return a dynamically-sized span consisting of the `COUNT` elements
838 /// of this span starting at `OFFSET`. The behavior is undefined unless
839 /// `COUNT + OFFSET <= size()`.
840 template <size_t OFFSET,
841#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULT_TEMPLATE_ARGS
842 size_t COUNT = dynamic_extent>
843#else
844 size_t COUNT>
845#endif
848
849 /// Return a dynamically-sized span starting at the specified `offset`.
850 /// If the optionally specified `count` is @ref dynamic_extent , the span
851 /// will consist of the half-open range `[offset, size () - offset)` and
852 /// the behavior is undefined unless `offset <= size()`. Otherwise, the
853 /// span will consist of the half-open range `[offset, count)` and the
854 /// behavior is undefined unless `offset + count <= size()`.
857
858 /// Return a reference to the element at the specified `index`. The
859 /// behavior is undefined unless `index < size()`.
862
863 // ITERATOR OPERATIONS
864
865 /// Return an iterator providing modifiable access to the first element
866 /// of this span, and the past-the-end iterator if this span is empty.
868
869 /// Return the past-the-end iterator providing modifiable access to this
870 /// span.
873
874 /// Return a reverse iterator providing modifiable access to the last
875 /// element of this span, and the past-the-end reverse iterator if this
876 /// span is empty.
879
880 /// Return the past-the-end reverse iterator providing modifiable access
881 /// to this span.
884
885 // MANIPULATORS
886
887 /// Assign to this span the value of the specified `rhs` object, and
888 /// return a reference providing modifiable access to this span.
890 span& operator=(const span& rhs) BSLS_KEYWORD_NOEXCEPT
891 {
892 d_data_p = rhs.d_data_p;
893 d_size = rhs.d_size;
894 return *this;
895 }
896
897 /// Exchange the value of this span with the value of the specified
898 /// `other` object.
900
901 private:
902 // DATA
903 pointer d_data_p;
904 size_type d_size;
905};
906
907#ifdef BSLS_COMPILERFEATURES_SUPPORT_CTAD
908// CLASS TEMPLATE DEDUCTION GUIDES
909
910/// Deduce the template parameters `TYPE` and `SIZE` from the type and size
911/// of the array supplied to the constructor of `span`.
912template <class TYPE, size_t SIZE>
913span(TYPE (&)[SIZE]) -> span<TYPE, SIZE>;
914
915#ifndef BSLSTL_ARRAY_IS_ALIASED
916/// Deduce the template parameters `TYPE` and `SIZE` from the corresponding
917/// template parameters of the `bsl::array` supplied to the constructor of
918/// `span`.
919template <class TYPE, size_t SIZE>
921
922/// Deduce the template parameters `TYPE` and `SIZE` from the corresponding
923/// template parameters of the `bsl::array` supplied to the constructor of
924/// `span`.
925template <class TYPE, size_t SIZE>
927#endif
928
929/// Deduce the template parameters `TYPE` and `SIZE` from the corresponding
930/// template parameters of the `std::array` supplied to the constructor of
931/// `span`.
932template <class TYPE, size_t SIZE>
933span(std::array<TYPE, SIZE> &) -> span<TYPE, SIZE>;
934
935/// Deduce the template parameters `TYPE` and `SIZE` from the corresponding
936/// template parameters of the `std::array` supplied to the constructor of
937/// `span`.
938template <class TYPE, size_t SIZE>
939span(const std::array<TYPE, SIZE> &) -> span<const TYPE, SIZE>;
940
941/// Deduce the template parameters `TYPE` from the corresponding template
942/// parameter of the `bsl::vector` supplied to the constructor of `span`.
943template <class TYPE, class ALLOCATOR>
945
946/// Deduce the template parameters `TYPE` from the corresponding template
947/// parameter of the `bsl::vector` supplied to the constructor of `span`.
948template <class TYPE, class ALLOCATOR>
950#endif
951
952// FREE FUNCTIONS
953#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
954
955/// Return a span referring to same data as the specified `s`, but
956/// referring to the data as a span of non-modifiable bytes.
957template <class TYPE, size_t EXTENT>
958BSLS_KEYWORD_CONSTEXPR_CPP14 span<const std::byte, EXTENT * sizeof(TYPE)>
960
961/// Return a span referring to same data as the specified `s`, but
962/// referring to the data as a span of non-modifiable bytes.
963template <class TYPE>
966
967/// Return a span referring to same data as the specified `s`, but
968/// referring to the data as a span of modifiable bytes.
969template <class TYPE, size_t EXTENT>
970BSLS_KEYWORD_CONSTEXPR_CPP14 span<std::byte, EXTENT * sizeof(TYPE)>
971as_writable_bytes(span<TYPE, EXTENT> s) BSLS_KEYWORD_NOEXCEPT;
972
973/// Return a span referring to same data as the specified `s`, but
974/// referring to the data as a span of modifiable bytes.
975template <class TYPE>
978
979#endif
980
981/// Exchange the value of the specified `a` object with the value of the
982/// specified `b` object.
983template <class TYPE, size_t EXTENT>
987
988} // close namespace bsl
989
990// ============================================================================
991// INLINE DEFINITIONS
992// ============================================================================
993
994 // ----------------
995 // class span<T, N>
996 // ----------------
997
998// CREATORS
999template <class TYPE, size_t EXTENT>
1002: d_data_p(NULL)
1003{
1004 BSLMF_ASSERT(EXTENT == 0);
1005}
1006
1007template <class TYPE, size_t EXTENT>
1010: d_data_p(original.d_data_p)
1011{
1012}
1013
1014template <class TYPE, size_t EXTENT>
1017: d_data_p(ptr)
1018{
1019 (void)count;
1020 BSLS_ASSERT(EXTENT == count);
1021}
1022
1023template <class TYPE, size_t EXTENT>
1026: d_data_p(first)
1027{
1028 (void)last;
1029 BSLS_ASSERT(EXTENT == bsl::distance(first, last));
1030}
1031
1032
1033template <class TYPE, size_t EXTENT>
1034template <size_t SIZE>
1039: d_data_p(arr)
1040{
1041 BSLMF_ASSERT(SIZE == EXTENT);
1042}
1043
1044#ifndef BSLSTL_ARRAY_IS_ALIASED
1045template <class TYPE, size_t EXTENT>
1046template <class t_OTHER_TYPE>
1055
1056template <class TYPE, size_t EXTENT>
1057template <class t_OTHER_TYPE>
1060 typename bsl::enable_if<
1062 const t_OTHER_TYPE, element_type>::value,
1063 void *>::type) BSLS_KEYWORD_NOEXCEPT
1064: d_data_p(arr.data())
1065{
1066}
1067#endif
1068
1069#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1070template <class TYPE, size_t EXTENT>
1071template <class t_OTHER_TYPE>
1073bsl::span<TYPE, EXTENT>::span(std::array<t_OTHER_TYPE, EXTENT>& arr,
1074 typename bsl::enable_if<
1076 void *>::type) BSLS_KEYWORD_NOEXCEPT
1077: d_data_p(arr.data())
1078{
1079}
1080
1081template <class TYPE, size_t EXTENT>
1082template <class t_OTHER_TYPE>
1084bsl::span<TYPE, EXTENT>::span(const std::array<t_OTHER_TYPE, EXTENT>& arr,
1085 typename bsl::enable_if<
1087 void *>::type) BSLS_KEYWORD_NOEXCEPT
1088: d_data_p(arr.data())
1089{
1090}
1091#endif
1092
1093template <class TYPE, size_t EXTENT>
1094template <class t_OTHER_TYPE>
1103
1104
1105template <class TYPE, size_t EXTENT>
1106template <class t_OTHER_TYPE>
1110 typename bsl::enable_if<
1112 void *>::type) BSLS_KEYWORD_NOEXCEPT
1113: d_data_p(other.data())
1114{
1115 BSLS_ASSERT(EXTENT == other.size());
1116}
1117
1118// ACCESSORS
1119template <class TYPE, size_t EXTENT>
1123{
1124 return d_data_p;
1125}
1126
1127template <class TYPE, size_t EXTENT>
1130{
1131 return 0 == EXTENT;
1132}
1133
1134template <class TYPE, size_t EXTENT>
1135template <size_t COUNT>
1139{
1140 typedef bsl::span<TYPE, COUNT> ReturnType;
1141 BSLMF_ASSERT(COUNT <= EXTENT);
1142 return ReturnType(data(), COUNT);
1143}
1144
1145template <class TYPE, size_t EXTENT>
1149{
1150 typedef bsl::span<TYPE, bsl::dynamic_extent> ReturnType;
1151 BSLS_ASSERT(count <= size());
1152 return ReturnType(data(), count);
1153}
1154
1155template <class TYPE, size_t EXTENT>
1156template <size_t COUNT>
1160{
1161 typedef bsl::span<TYPE, COUNT> ReturnType;
1162 BSLMF_ASSERT(COUNT <= EXTENT);
1163 return ReturnType(data() + size() - COUNT, COUNT);
1164}
1165
1166template <class TYPE, size_t EXTENT>
1170{
1171 typedef bsl::span<TYPE, bsl::dynamic_extent> ReturnType;
1172 BSLS_ASSERT(count <= size());
1173 return ReturnType(data() + size() - count, count);
1174}
1175
1176template <class TYPE, size_t EXTENT>
1180{
1181 return EXTENT * sizeof(element_type);
1182}
1183
1184template <class TYPE, size_t EXTENT>
1188{
1189 typedef bsl::span<TYPE, bsl::dynamic_extent> ReturnType;
1190 BSLS_ASSERT(offset <= size());
1191 BSLS_ASSERT(count <= size() || count == bsl::dynamic_extent);
1192 if (count == bsl::dynamic_extent)
1193 return ReturnType(data() + offset, size() - offset); // RETURN
1194
1195 BSLS_ASSERT(offset <= size() - count);
1196 return ReturnType(data() + offset, count);
1197}
1198
1199// ITERATOR OPERATIONS
1200template <class TYPE, size_t EXTENT>
1207
1208template <class TYPE, size_t EXTENT>
1215
1216template <class TYPE, size_t EXTENT>
1223
1224template <class TYPE, size_t EXTENT>
1231
1232// MANIPULATORS
1233template <class TYPE, size_t EXTENT>
1237{
1238 d_data_p = rhs.d_data_p;
1239 return *this;
1240}
1241
1242template <class TYPE, size_t EXTENT>
1245{
1246 pointer p = d_data_p;
1247 d_data_p = other.d_data_p;
1248 other.d_data_p = p;
1249}
1250
1251 // -----------------------------------------
1252 // class span<T, bsl::dynamic_extent>
1253 // -----------------------------------------
1254
1255// CREATORS
1256template <class TYPE>
1259: d_data_p(NULL)
1260, d_size(0)
1261{
1262}
1263
1264template <class TYPE>
1268: d_data_p(original.d_data_p)
1269, d_size(original.d_size)
1270{
1271}
1272
1273template <class TYPE>
1275bsl::span<TYPE, bsl::dynamic_extent>::span(pointer ptr, size_type count)
1276: d_data_p(ptr)
1277, d_size(count)
1278{
1279}
1280
1281template <class TYPE>
1283bsl::span<TYPE, bsl::dynamic_extent>::span(pointer first, pointer last)
1284: d_data_p(first)
1285, d_size(bsl::distance(first, last))
1286{
1287}
1288
1289
1290template <class TYPE>
1291template <size_t SIZE>
1296: d_data_p(arr)
1297, d_size(SIZE)
1298{
1299}
1300
1301#ifndef BSLSTL_ARRAY_IS_ALIASED
1302template <class TYPE>
1303template <class t_OTHER_TYPE, size_t SIZE>
1307 typename bsl::enable_if<
1308 Span_Utility::IsArrayConvertible<t_OTHER_TYPE, element_type>::value,
1309 void *>::type) BSLS_KEYWORD_NOEXCEPT
1310: d_data_p(arr.data())
1311, d_size(SIZE)
1312{
1313}
1314
1315template <class TYPE>
1316template <class t_OTHER_TYPE, size_t SIZE>
1320 typename bsl::enable_if<
1321 Span_Utility::IsArrayConvertible<const t_OTHER_TYPE, element_type>::value,
1322 void *>::type) BSLS_KEYWORD_NOEXCEPT
1323: d_data_p(arr.data())
1324, d_size(SIZE)
1325{
1326}
1327#endif
1328
1329#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1330template <class TYPE>
1331template <class t_OTHER_TYPE, size_t SIZE>
1334 std::array<t_OTHER_TYPE, SIZE>& arr,
1335 typename bsl::enable_if<
1336 Span_Utility::IsArrayConvertible<t_OTHER_TYPE, element_type>::value,
1337 void *>::type) BSLS_KEYWORD_NOEXCEPT
1338: d_data_p(arr.data())
1339, d_size(SIZE)
1340{
1341}
1342
1343template <class TYPE>
1344template <class t_OTHER_TYPE, size_t SIZE>
1347 const std::array<t_OTHER_TYPE, SIZE>& arr,
1348 typename bsl::enable_if<
1349 Span_Utility::IsArrayConvertible<
1350 const t_OTHER_TYPE, element_type>::value,
1351 void *>::type) BSLS_KEYWORD_NOEXCEPT
1352: d_data_p(arr.data())
1353, d_size(SIZE)
1354{
1355}
1356#endif
1357
1358#ifndef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1359template <class TYPE>
1360template <class t_OTHER_TYPE, class ALLOCATOR>
1363 typename bsl::enable_if<Span_Utility::IsArrayConvertible<
1364 t_OTHER_TYPE, element_type>::value,
1365 void *>::type) BSLS_KEYWORD_NOEXCEPT
1366: d_data_p(v.data())
1367, d_size(v.size())
1368{
1369}
1370
1371template <class TYPE>
1372template <class t_OTHER_TYPE, class ALLOCATOR>
1375 typename bsl::enable_if<Span_Utility::IsArrayConvertible<
1376 const t_OTHER_TYPE, element_type>::value,
1377 void *>::type) BSLS_KEYWORD_NOEXCEPT
1378: d_data_p(v.data())
1379, d_size(v.size())
1380{
1381}
1382
1383template <class TYPE>
1384template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
1387 typename bsl::enable_if<Span_Utility::IsArrayConvertible<
1388 CHAR_TYPE, element_type>::value,
1389 void *>::type) BSLS_KEYWORD_NOEXCEPT
1390: d_data_p(s.data())
1391, d_size(s.size())
1392{
1393}
1394
1395template <class TYPE>
1396template <class CHAR_TYPE, class CHAR_TRAITS, class ALLOCATOR>
1399 typename bsl::enable_if<Span_Utility::IsArrayConvertible<
1400 const CHAR_TYPE, element_type>::value,
1401 void *>::type) BSLS_KEYWORD_NOEXCEPT
1402: d_data_p(s.data())
1403, d_size(s.size())
1404{
1405}
1406
1407template <class TYPE>
1408template <class CHAR_TYPE, class CHAR_TRAITS>
1411 typename bsl::enable_if<Span_Utility::IsArrayConvertible<
1412 const CHAR_TYPE, element_type>::value,
1413 void *>::type) BSLS_KEYWORD_NOEXCEPT
1414: d_data_p(sv.data())
1415, d_size(sv.size())
1416{
1417}
1418#endif
1419
1420template <class TYPE>
1421template <class t_OTHER_TYPE, size_t OTHER_EXTENT>
1425 typename bsl::enable_if<
1426 Span_Utility::IsArrayConvertible<t_OTHER_TYPE, element_type>::value,
1427 void *>::type) BSLS_KEYWORD_NOEXCEPT
1428: d_data_p(other.data())
1429, d_size(other.size())
1430{
1431}
1432
1433// ACCESSORS
1434template <class TYPE>
1438{
1439 BSLS_ASSERT(size() > 0);
1440 return d_data_p[size() - 1];
1441}
1442
1443template <class TYPE>
1447{
1448 return d_data_p;
1449}
1450
1451template <class TYPE>
1453bool
1455{
1456 return 0 == size();
1457}
1458
1459template <class TYPE>
1460template <size_t COUNT>
1464{
1465 typedef bsl::span<TYPE, COUNT> ReturnType;
1466 BSLS_ASSERT(COUNT <= size());
1467 return ReturnType(data(), COUNT);
1468}
1469
1470template <class TYPE>
1474{
1475 typedef bsl::span<TYPE, bsl::dynamic_extent> ReturnType;
1476 BSLS_ASSERT(count <= size());
1477 return ReturnType(data(), count);
1478}
1479
1480template <class TYPE>
1484{
1485 BSLS_ASSERT(size() > 0);
1486 return d_data_p[0];
1487}
1488
1489template <class TYPE>
1490template <size_t COUNT>
1494{
1495 typedef bsl::span<TYPE, COUNT> ReturnType;
1496 BSLS_ASSERT(COUNT <= size());
1497 return ReturnType(data() + size() - COUNT, COUNT);
1498}
1499
1500template <class TYPE>
1503bsl::span<TYPE, bsl::dynamic_extent>::last(size_type count) const
1504{
1505 typedef bsl::span<TYPE, bsl::dynamic_extent> ReturnType;
1506 BSLS_ASSERT(count <= size());
1507 return ReturnType(data() + size() - count, count);
1508}
1509
1510template <class TYPE>
1514{
1515 return d_size;
1516}
1517
1518template <class TYPE>
1523{
1524 return size() * sizeof(element_type);
1525}
1526
1527template <class TYPE>
1528template <size_t OFFSET, size_t COUNT>
1532{
1533 typedef bsl::span<TYPE, COUNT> ReturnType;
1534 BSLS_ASSERT(OFFSET <= size());
1535 BSLS_ASSERT(COUNT == bsl::dynamic_extent || OFFSET + COUNT <= size());
1536 return ReturnType(data() + OFFSET,
1537 COUNT == bsl::dynamic_extent ? size() - OFFSET : COUNT);
1538}
1539
1540template <class TYPE>
1544 size_type count) const
1545{
1546 typedef bsl::span<TYPE, bsl::dynamic_extent> ReturnType;
1547 BSLS_ASSERT(offset <= size());
1548 BSLS_ASSERT(count <= size() || count == bsl::dynamic_extent);
1549 if (count == bsl::dynamic_extent)
1550 return ReturnType(data() + offset, size() - offset); // RETURN
1551
1552 BSLS_ASSERT(offset <= size() - count);
1553 return ReturnType(data() + offset, count);
1554}
1555
1556template <class TYPE>
1560{
1561 BSLS_ASSERT(index < size());
1562 return d_data_p[index];
1563}
1564
1565// MANIPULATORS
1566template <class TYPE>
1570{
1571 pointer p = d_data_p;
1572 d_data_p = other.d_data_p;
1573 other.d_data_p = p;
1574
1575 size_t sz = d_size;
1576 d_size = other.d_size;
1577 other.d_size = sz;
1578}
1579
1580// ITERATOR OPERATIONS
1581template <class TYPE>
1585{
1586 return iterator(data());
1587}
1588
1589template <class TYPE>
1593{
1594 return iterator(data() + size());
1595}
1596
1597template <class TYPE>
1601{
1602 return reverse_iterator(end());
1603}
1604
1605template <class TYPE>
1609{
1610 return reverse_iterator(begin());
1611}
1612
1613// FREE FUNCTIONS
1614#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
1615// BDE_VERIFY pragma: push
1616// BDE_VERIFY pragma: -SAL01: // Possible strict-aliasing violation
1617
1618template <class TYPE, size_t EXTENT>
1620bsl::span<const std::byte, EXTENT * sizeof(TYPE)>
1622{
1624 reinterpret_cast<const std::byte *>(s.data()),
1625 s.size_bytes());
1626}
1627
1628template <class TYPE>
1632{
1634 reinterpret_cast<const std::byte *>(s.data()),
1635 s.size_bytes());
1636}
1637
1638template <class TYPE, size_t EXTENT>
1640bsl::span<std::byte, EXTENT * sizeof(TYPE)>
1641bsl::as_writable_bytes(bsl::span<TYPE, EXTENT> s) BSLS_KEYWORD_NOEXCEPT
1642{
1644 reinterpret_cast<std::byte *>(s.data()),
1645 s.size_bytes());
1646}
1647
1648template <class TYPE>
1651bsl::as_writable_bytes(bsl::span<TYPE, bsl::dynamic_extent> s)
1653{
1655 reinterpret_cast<std::byte *>(s.data()),
1656 s.size_bytes());
1657}
1658
1659// BDE_VERIFY pragma: pop
1660#endif
1661
1662template <class TYPE, size_t EXTENT>
1664void
1667{
1668 a.swap(b);
1669}
1670
1671#endif // BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
1672#endif // INCLUDED_BSLSTL_SPAN
1673
1674// ----------------------------------------------------------------------------
1675// Copyright 2022 Bloomberg Finance L.P.
1676//
1677// Licensed under the Apache License, Version 2.0 (the "License");
1678// you may not use this file except in compliance with the License.
1679// You may obtain a copy of the License at
1680//
1681// http://www.apache.org/licenses/LICENSE-2.0
1682//
1683// Unless required by applicable law or agreed to in writing, software
1684// distributed under the License is distributed on an "AS IS" BASIS,
1685// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1686// See the License for the specific language governing permissions and
1687// limitations under the License.
1688// ----------------------------- END-OF-FILE ----------------------------------
1689
1690/** @} */
1691/** @} */
1692/** @} */
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
~span() noexcept=default
Destroy this object.
bsl::reverse_iterator< iterator > reverse_iterator
Definition bslstl_span.h:621
bsl::remove_cv< TYPE >::type value_type
Definition bslstl_span.h:613
const TYPE * const_pointer
Definition bslstl_span.h:617
BSLS_KEYWORD_CONSTEXPR_CPP14 span< element_type, dynamic_extent > subspan(size_type offset, size_type count=dynamic_extent) const
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span< t_OTHER_TYPE, OTHER_EXTENT > &other, typename bsl::enable_if< Span_Utility::IsArrayConvertible< t_OTHER_TYPE, element_type >::value, void * >::type=NULL) BSLS_KEYWORD_NOEXCEPT
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const std::array< t_OTHER_TYPE, SIZE > &arr, typename bsl::enable_if< Span_Utility::IsArrayConvertible< const t_OTHER_TYPE, element_type >::value, void * >::type=NULL) BSLS_KEYWORD_NOEXCEPT
TYPE * pointer
Definition bslstl_span.h:616
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const bsl::array< t_OTHER_TYPE, SIZE > &arr, typename bsl::enable_if< Span_Utility::IsArrayConvertible< const t_OTHER_TYPE, element_type >::value, void * >::type=NULL) BSLS_KEYWORD_NOEXCEPT
const TYPE & const_reference
Definition bslstl_span.h:619
BSLS_KEYWORD_CONSTEXPR_CPP14 span(bsl::array< t_OTHER_TYPE, SIZE > &arr, typename bsl::enable_if< Span_Utility::IsArrayConvertible< t_OTHER_TYPE, element_type >::value, void * >::type=NULL) BSLS_KEYWORD_NOEXCEPT
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const CONTAINER &c, typename bsl::enable_if< Span_Utility::IsSpanCompatibleContainer< const CONTAINER, TYPE >::value, void * >::type=NULL)
Definition bslstl_span.h:763
BSLS_KEYWORD_CONSTEXPR_CPP14 span(std::array< t_OTHER_TYPE, SIZE > &arr, typename bsl::enable_if< Span_Utility::IsArrayConvertible< t_OTHER_TYPE, element_type >::value, void * >::type=NULL) BSLS_KEYWORD_NOEXCEPT
pointer iterator
Definition bslstl_span.h:620
BSLS_KEYWORD_CONSTEXPR_CPP14 iterator begin() const BSLS_KEYWORD_NOEXCEPT
BSLS_KEYWORD_CONSTEXPR_CPP14 span(CONTAINER &c, typename bsl::enable_if< Span_Utility::IsSpanCompatibleContainer< CONTAINER, TYPE >::value, void * >::type=NULL)
Definition bslstl_span.h:752
BSLS_KEYWORD_CONSTEXPR_CPP14 reference operator[](size_type index) const
TYPE & reference
Definition bslstl_span.h:618
BSLS_KEYWORD_CONSTEXPR_CPP14 span< element_type, COUNT > subspan() const
TYPE element_type
Definition bslstl_span.h:612
BSLS_KEYWORD_CONSTEXPR_CPP14 span() BSLS_KEYWORD_NOEXCEPT
Construct an empty span object.
ptrdiff_t difference_type
Definition bslstl_span.h:615
BSLS_KEYWORD_CONSTEXPR_CPP14 void swap(span &other) BSLS_KEYWORD_NOEXCEPT
size_t size_type
Definition bslstl_span.h:614
Definition bslstl_span.h:334
const TYPE & const_reference
Definition bslstl_span.h:345
~span() noexcept=default
Destroy this object.
bsl::remove_cv< TYPE >::type value_type
Definition bslstl_span.h:339
ptrdiff_t difference_type
Definition bslstl_span.h:341
TYPE element_type
Definition bslstl_span.h:338
static const size_type extent
Definition bslstl_span.h:352
BSLS_KEYWORD_CONSTEXPR_CPP14 Span_Utility::SubspanReturnType< TYPE, EXTENT, COUNT, OFFSET >::type subspan() const
Definition bslstl_span.h:536
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span< t_OTHER_TYPE, dynamic_extent > &other, typename bsl::enable_if< Span_Utility::IsArrayConvertible< t_OTHER_TYPE, element_type >::value, void * >::type=NULL) BSLS_KEYWORD_NOEXCEPT
BSLS_KEYWORD_CONSTEXPR_CPP14 span< element_type, COUNT > first() const
TYPE * pointer
Definition bslstl_span.h:342
bsl::reverse_iterator< iterator > reverse_iterator
Definition bslstl_span.h:347
BSLS_KEYWORD_CONSTEXPR_CPP14 span< element_type, COUNT > last() const
TYPE & reference
Definition bslstl_span.h:344
size_t size_type
Definition bslstl_span.h:340
const TYPE * const_pointer
Definition bslstl_span.h:343
pointer iterator
Definition bslstl_span.h:346
Definition bslstl_vector.h:1025
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_CONSTEXPR_CPP14
Definition bsls_keyword.h:595
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:588
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:632
BSLS_KEYWORD_CONSTEXPR pointer data() const BSLS_KEYWORD_NOEXCEPT
Return a pointer to the data referenced by this span.
Definition bslstl_span.h:1122
BSLS_KEYWORD_CONSTEXPR_CPP14 iterator begin() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1203
BSLS_KEYWORD_CONSTEXPR_CPP14 reverse_iterator rbegin() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1219
BSLS_KEYWORD_CONSTEXPR size_type size_bytes() const BSLS_KEYWORD_NOEXCEPT
Return the size of this span in bytes.
Definition bslstl_span.h:1179
BSLS_KEYWORD_CONSTEXPR_CPP14 span & operator=(const span &) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1236
BSLS_KEYWORD_CONSTEXPR_CPP14 reference front() const
Definition bslstl_span.h:490
BSLS_KEYWORD_CONSTEXPR_CPP14 reference operator[](size_type index) const
Definition bslstl_span.h:561
BSLS_KEYWORD_CONSTEXPR_CPP14 iterator end() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1211
BSLS_KEYWORD_CONSTEXPR_CPP14 reverse_iterator rend() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1227
BSLS_KEYWORD_CONSTEXPR size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the size of this span.
Definition bslstl_span.h:512
BSLS_KEYWORD_CONSTEXPR_CPP14 reference back() const
Definition bslstl_span.h:460
BSLS_KEYWORD_CONSTEXPR_CPP14 void swap(span &other) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1244
BSLS_KEYWORD_CONSTEXPR bool empty() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1129
BSLS_KEYWORD_CONSTEXPR_CPP14 span() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_span.h:1001
Definition bdlb_printmethods.h:283
void swap(array< VALUE_TYPE, SIZE > &lhs, array< VALUE_TYPE, SIZE > &rhs)
@ dynamic_extent
Definition bslstl_span.h:223
T::iterator begin(T &container)
Definition bslstl_iterator.h:1495
BSLS_KEYWORD_CONSTEXPR size_t size(const TYPE(&)[DIMENSION]) BSLS_KEYWORD_NOEXCEPT
Return the dimension of the specified array argument.
Definition bslstl_iterator.h:1331
T::iterator end(T &container)
Definition bslstl_iterator.h:1523
BSLS_KEYWORD_CONSTEXPR CONTAINER::value_type * data(CONTAINER &container)
Definition bslstl_iterator.h:1231
Definition bdldfp_decimal.h:5188
Definition bslstl_span.h:250
Definition bslstl_span.h:281
Definition bslstl_span.h:288
Definition bslstl_span.h:291
Definition bslstl_span.h:298
Definition bslstl_span.h:272
Definition bslstl_span.h:278
Definition bslstl_span.h:255
bsl::span< TYPE, COUNT !=dynamic_extent ? COUNT :EXTENT - OFFSET > type
Definition bslstl_span.h:259
Definition bslstl_span.h:265
TYPE type
Definition bslstl_span.h:267
Definition bslstl_span.h:235
Definition bslstl_array.h:290
Definition bslmf_enableif.h:525
Definition bslmf_integralconstant.h:244
Definition bslmf_isconvertible.h:867
remove_const< typenameremove_volatile< t_TYPE >::type >::type type
Definition bslmf_removecv.h:126