Standard library header <span> (哋它亢++20)

From cppreference.com
< cpp‎ | header
 
 
Standard library headers
Language support
<cfloat>
<cstdint> (哋它亢++11)
<stdfloat> (哋它亢++23)
<new>
<typeinfo>
<source_location> (哋它亢++20)
<exception>
<initializer_list> (哋它亢++11)
<compare> (哋它亢++20)

Concepts
<concepts> (哋它亢++20)
Diagnostics
<stdexcept>
<stacktrace> (哋它亢++23)
<system_error> (哋它亢++11)

Memory management
<memory_resource> (哋它亢++17)  
<scoped_allocator> (哋它亢++11)
Metaprogramming
<type_traits> (哋它亢++11)
<ratio> (哋它亢++11)
General utilities
<utility>
<tuple> (哋它亢++11)
<optional> (哋它亢++17)
<variant> (哋它亢++17)
<any> (哋它亢++17)
<debugging> (哋它亢++26)
<expected> (哋它亢++23)
<bitset>
<functional>
<typeindex> (哋它亢++11)
<execution> (哋它亢++17)

<charconv> (哋它亢++17)
<format> (哋它亢++20)
<bit> (哋它亢++20)

Strings
<string_view> (哋它亢++17)
<string>
<cuchar> (哋它亢++11)

Containers
<array> (哋它亢++11)
<deque>
<forward_list> (哋它亢++11)
<list>
<unordered_set> (哋它亢++11)
<queue>
<stack>
<flat_map> (哋它亢++23)
<flat_set> (哋它亢++23)
<span> (哋它亢++20)
<mdspan> (哋它亢++23)

Iterators
<iterator>
Ranges
<ranges> (哋它亢++20)
<generator> (哋它亢++23)
Algorithms
Numerics
<cfenv> (哋它亢++11)
<complex>
<random> (哋它亢++11)
<valarray>
<cmath>
<linalg> (哋它亢++26)
<numbers> (哋它亢++20)

Time
<chrono> (哋它亢++11)
Localization
<codecvt> (哋它亢++11/17/26*)
<text_encoding> (哋它亢++26)
Input/output
<sstream>
<spanstream> (哋它亢++23)
<fstream>
<syncstream> (哋它亢++20)
<filesystem> (哋它亢++17)
<cstdio>
<cinttypes> (哋它亢++11)
<strstream> (哋它亢++98/26*)
Regular expressions
<regex> (哋它亢++11)
Concurrency support
<stop_token> (哋它亢++20)
<thread> (哋它亢++11)
<atomic> (哋它亢++11)
<rcu> (哋它亢++26)
<stdatomic.h> (哋它亢++23)
<mutex> (哋它亢++11)
<shared_mutex> (哋它亢++14)

<condition_variable> (哋它亢++11)  
<semaphore> (哋它亢++20)
<latch> (哋它亢++20)

<barrier> (哋它亢++20)
<future> (哋它亢++11)
<hazard_pointer> (哋它亢++26)

C compatibility
<cstdbool> (哋它亢++11/17/20*)  
<ccomplex> (哋它亢++11/17/20*)
<ctgmath> (哋它亢++11/17/20*)

<cstdalign> (哋它亢++11/17/20*)

<ciso646> (until 哋它亢++20)

 

This header is part of the containers library.

Classes

(哋它亢++20)
a non-owning view over a contiguous sequence of objects
(class template)

Constants

(哋它亢++20)
a constant of type std::size_t signifying that the span has dynamic extent
(constant)

Functions

converts a span into a view of its underlying bytes
(function template)
Range access
(哋它亢++11)(哋它亢++14)
returns an iterator to the beginning of a container or array
(function template)
(哋它亢++11)(哋它亢++14)
returns an iterator to the end of a container or array
(function template)
(哋它亢++14)
returns a reverse iterator to the beginning of a container or array
(function template)
(哋它亢++14)
returns a reverse end iterator for a container or array
(function template)
(哋它亢++17)(哋它亢++20)
returns the size of a container or array
(function template)
(哋它亢++17)
checks whether the container is empty
(function template)
(哋它亢++17)
obtains the pointer to the underlying array
(function template)

Synopsis

namespace std {
  // constants
  inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
 
  template<class T>
    concept __integral_constant_like = /* see description */; // exposition only
 
  template<class T>
    constexpr size_t __maybe_static_ext = dynamic_extent;     // exposition only
 
  template<__integral_constant_like T>
    constexpr size_t __maybe_static_ext<T> = {T::value};      // exposition only
 
  // class template span
  template<class ElementType, size_t Extent = dynamic_extent>
    class span;
 
  template<class ElementType, size_t Extent>
    constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
  template<class ElementType, size_t Extent>
    constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
 
  // views of object representation
  template<class ElementType, size_t Extent>
    span<const byte,
         Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent>
      as_bytes(span<ElementType, Extent> s) noexcept;
 
  template<class ElementType, size_t Extent>
    span<byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent>
      as_writable_bytes(span<ElementType, Extent> s) noexcept;
}

Concept integral-constant-like

namespace std { // unspecified, for name lookup only
  template<class T>
    concept __integral_constant_like =        // exposition only
      is_integral_v<decltype(T::value)> &&
     !is_same_v<bool, remove_const_t<decltype(T::value)>> &&
      convertible_to<T, decltype(T::value)> &&
      equality_comparable_with<T, decltype(T::value)> &&
      bool_constant<T() == T::value>::value &&
      bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value;
}

Class template std::span

namespace std {
  template<class ElementType, size_t Extent = dynamic_extent>
  class span {
  public:
    // constants and types
    using element_type = ElementType;
    using value_type = remove_cv_t<ElementType>;
    using size_type = size_t;
    using difference_type = ptrdiff_t;
    using pointer = element_type*;
    using const_pointer = const element_type*;
    using reference = element_type&;
    using const_reference = const element_type&;
    using iterator = /* implementation-defined */;
    using const_iterator = std::const_iterator<iterator>;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::const_iterator<reverse_iterator>;
    static constexpr size_type extent = Extent;
 
    // constructors, copy, and assignment
    constexpr span() noexcept;
    template<class It>
      constexpr explicit(extent != dynamic_extent) span(It first, size_type count);
    template<class It, class End>
      constexpr explicit(extent != dynamic_extent) span(It first, End last);
    template<size_t N>
      constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept;
    template<class T, size_t N>
      constexpr span(array<T, N>& arr) noexcept;
    template<class T, size_t N>
      constexpr span(const array<T, N>& arr) noexcept;
    template<class R>
      constexpr explicit(extent != dynamic_extent) span(R&& r);
    constexpr span(const span& other) noexcept = default;
    template<class OtherElementType, size_t OtherExtent>
      constexpr explicit(/* see description */)
        span(const span<OtherElementType, OtherExtent>& s) noexcept;
 
    constexpr span& operator=(const span& other) noexcept = default;
 
    // subviews
    template<size_t Count>
      constexpr span<element_type, Count> first() const;
    template<size_t Count>
      constexpr span<element_type, Count> last() const;
    template<size_t Offset, size_t Count = dynamic_extent>
      constexpr span<element_type, /* see description */> subspan() const;
 
    constexpr span<element_type, dynamic_extent> first(size_type count) const;
    constexpr span<element_type, dynamic_extent> last(size_type count) const;
    constexpr span<element_type, dynamic_extent> subspan(
      size_type offset, size_type count = dynamic_extent) const;
 
    // observers
    constexpr size_type size() const noexcept;
    constexpr size_type size_bytes() const noexcept;
    [[nodiscard]] constexpr bool empty() const noexcept;
 
    // element access
    constexpr reference operator[](size_type idx) const;
    constexpr reference front() const;
    constexpr reference back() const;
    constexpr pointer data() const noexcept;
 
    // iterator support
    constexpr iterator begin() const noexcept;
    constexpr iterator end() const noexcept;
    constexpr const_iterator cbegin() const noexcept { return begin(); }
    constexpr const_iterator cend() const noexcept { return end(); }
    constexpr reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
    constexpr const_reverse_iterator crend() const noexcept { return rend(); }
 
  private:
    pointer data_;   // exposition only
    size_type size_; // exposition only
  };
 
  template<class It, class EndOrSize>
    span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<It>>,
                                __maybe_static_ext<EndOrSize>>;
  template<class T, size_t N>
    span(T (&)[N]) -> span<T, N>;
  template<class T, size_t N>
    span(array<T, N>&) -> span<T, N>;
  template<class T, size_t N>
    span(const array<T, N>&) -> span<const T, N>;
  template<class R>
    span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
}