标准库标头 <span> (C++20)

来自cppreference.com
< cpp‎ | header


 
 
标准库标头
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用工具
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

日期时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (C++20 前)

 

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

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

常量

std::size_t 类型的常量,指明 span 拥有动态长度
(常量)

函数

转换 span 为对其底层字节的视图
(函数模板)
范围访问
(C++11)(C++14)
返回指向容器或数组起始的迭代器
(函数模板)
(C++11)(C++14)
返回指向容器或数组结尾的迭代器
(函数模板)
返回指向一个容器或数组的逆向迭代器
(函数模板)
(C++14)
返回容器或数组的逆向尾迭代器
(函数模板)
(C++17)(C++20)
返回容器或数组的大小
(函数模板)
(C++17)
检查容器是否为空
(函数模板)
(C++17)
获得指向底层数组的指针
(函数模板)

概要

namespace std {
  // 常量
  inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
 
  template<class T>
    concept __integral_constant_like = /* 见描述 */;           // 仅用于阐释
 
  template<class T>
    constexpr size_t __maybe_static_ext = dynamic_extent;     // 仅用于阐释
 
  template<__integral_constant_like T>
    constexpr size_t __maybe_static_ext<T> = {T::value};      // 仅用于阐释
 
  // 类模板 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;
 
  // 对象表示的视图
  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;
}

概念 integral-constant-like

namespace std { // 未指明,仅用于名字查找
  template<class T>
    concept __integral_constant_like =        // 仅用于阐释
      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;
}

类模板 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 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;
 
    // 构造函数、复制与赋值
    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(/* 见描述 */)
        span(const span<OtherElementType, OtherExtent>& s) noexcept;
 
    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 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;
 
    // 观察器
    constexpr size_type size() const noexcept;
    constexpr size_type size_bytes() const noexcept;
    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 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_;   // 仅用于阐述
    size_type size_; // 仅用于阐述
  };
 
  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>>>;
}