标准库头文件 <span>

来自cppreference.com
< cpp‎ | header
 
 
 

此头文件是容器库的一部分。

目录

[编辑]

(C++20)
对象的连续序列上的无所有权视图
(类模板) [编辑]

[编辑] 函数

转换 span 为对其底层字节的视图
(函数模板) [编辑]

[编辑] 常量

size_t 类型常量,指明 span 拥有动态长度
(常量) [编辑]

[编辑] 概要

namespace std {
  // 常量
  inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
 
  // 类模板 span
  template<class ElementType, size_t Extent = dynamic_extent>
    class span;
 
  template<class ElementType, size_t Extent>
    inline constexpr bool ranges::enable_safe_range<span<ElementType, Extent>> = true;
 
  // 对象表示的视图
  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;
}

[编辑] 类模板 std::span

namespace std {
  template<class ElementType, size_t Extent = dynamic_extent>
  class span {
  public:
    // 常量与类型
    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 = /* 实现定义 */;
    using reverse_iterator = std::reverse_iterator<iterator>;
    static constexpr size_type extent = Extent;
 
    // 构造函数、复制与赋值
    constexpr span() noexcept;
    template<class It>
      constexpr span(It first, size_type count);
    template<class It, class End>
      constexpr span(It first, End last);
    template<size_t N>
      constexpr span(element_type (&arr)[N]) noexcept;
    template<size_t N>
      constexpr span(array<value_type, N>& arr) noexcept;
    template<size_t N>
      constexpr span(const array<value_type, N>& arr) noexcept;
    template<class R>
      constexpr span(R&& r);
    constexpr span(const span& other) noexcept = default;
    template<class OtherElementType, size_t OtherExtent>
      constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
 
    ~span() noexcept = default;
 
    constexpr span& operator=(const span& other) noexcept = default;
 
    // 子视图
    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 below> 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;
 
    // 观察器
    constexpr size_type size() const noexcept;
    constexpr size_type size_bytes() const noexcept;
    [[nodiscard]] constexpr bool empty() const noexcept;
 
    // 元素访问
    constexpr reference operator[](size_type idx) const;
    constexpr reference front() const;
    constexpr reference back() const;
    constexpr pointer data() const noexcept;
 
    // 迭代器支持
    constexpr iterator begin() const noexcept;
    constexpr iterator end() const noexcept;
    constexpr reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() const noexcept;
 
  private:
    pointer data_;              // 仅用于阐释
    size_type size_;            // 仅用于阐释
  };
 
  template<class It, class End>
    span(It, End) -> span<remove_reference_t<iter_reference_t<It>>>;
  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>>>;
}