Files
2025-10-25 03:02:53 +03:00

30 KiB
Raw Permalink Blame History

[views.span]

23 Containers library [containers]

23.7 Views [views]

23.7.2 Contiguous access [views.contiguous]

23.7.2.2 Class template span [views.span]

23.7.2.2.1 Overview [span.overview]

1

#

A span is a view over a contiguous sequence of objects, the storage of which is owned by some other object.

2

#

All member functions of span have constant time complexity.

🔗

namespace std {template<class ElementType, size_t Extent = dynamic_extent>class span {public:// constants and typesusing element_type = ElementType; using value_type = remove_cv_t; 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; // see [span.iterators]using const_iterator = std::const_iterator; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::const_iterator<reverse_iterator>; static constexpr size_type extent = Extent; // [span.cons], constructors, copy, and assignmentconstexpr span() noexcept; templateconstexpr 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; templateconstexpr explicit(extent != dynamic_extent) span(R&& r); constexpr explicit(extent != dynamic_extent) span(std::initializer_list<value_type> il); constexpr span(const span& other) noexcept = default; template<class OtherElementType, size_t OtherExtent>constexpr explicit(see below) span(const span<OtherElementType, OtherExtent>& s) noexcept; constexpr span& operator=(const span& other) noexcept = default; // [span.sub], subviewstemplate<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; // [span.obs], observersconstexpr size_type size() const noexcept; constexpr size_type size_bytes() const noexcept; constexpr bool empty() const noexcept; // [span.elem], element accessconstexpr reference operator[](size_type idx) const; constexpr reference at(size_type idx) const; // freestanding-deletedconstexpr reference front() const; constexpr reference back() const; constexpr pointer data() const noexcept; // [span.iterators], iterator supportconstexpr 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>, maybe-static-ext>; 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 span(R&&) -> span<remove_reference_t<ranges::range_reference_t>>;}

3

#

span<ElementType, Extent> is a trivially copyable type ([basic.types.general]).

4

#

ElementType is required to be a complete object type that is not an abstract class type.

5

#

For a span s, any operation that invalidates a pointer in the range [s.data(), s.data() + s.size()) invalidates pointers, iterators, and references to elements of s.

23.7.2.2.2 Constructors, copy, and assignment [span.cons]

🔗

constexpr span() noexcept;

1

#

Constraints: Extent == dynamic_extent || Extent == 0 is true.

2

#

Postconditions: size() == 0 && data() == nullptr.

🔗

template<class It> constexpr explicit(extent != dynamic_extent) span(It first, size_type count);

3

#

Constraints: Let U be remove_reference_t<iter_reference_t>.

  • (3.1)

    It satisfies contiguous_iterator.

  • (3.2)

    is_convertible_v<U()[], element_type()[]> is true. [Note 1: The intent is to allow only qualification conversions of the iterator reference type to element_type. — end note]

4

#

Preconditions:

5

#

Hardened preconditions: If extent is not equal to dynamic_extent, then count == extent is true.

6

#

Effects: Initializes data_ with to_address(first) andsize_ with count.

7

#

Throws: Nothing.

🔗

template<class It, class End> constexpr explicit(extent != dynamic_extent) span(It first, End last);

8

#

Constraints: Let U be remove_reference_t<iter_reference_t>.

9

#

Preconditions:

10

#

Hardened preconditions: If extent is not equal to dynamic_extent, then (last - first) == extent is true.

11

#

Effects: Initializes data_ with to_address(first) andsize_ with last - first.

12

#

Throws: When and what last - first throws.

🔗

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;

13

#

Constraints: Let U be remove_pointer_t<decltype(std::data(arr))>.

extent == dynamic_extent || N == extent is true, and

is_convertible_v<U()[], element_type()[]> is true. [Note 3: The intent is to allow only qualification conversions of the array element type to element_type. — end note]

14

#

Effects: Constructs a span that is a view over the supplied array.

[Note 4:

type_identity_t affects class template argument deduction.

— end note]

15

#

Postconditions: size() == N && data() == std::data(arr) is true.

🔗

template<class R> constexpr explicit(extent != dynamic_extent) span(R&& r);

16

#

Constraints: Let U be remove_reference_t<ranges::range_reference_t>.

  • (16.1)

    R satisfies ranges::contiguous_range and ranges::sized_range.

  • (16.2)

    Either R satisfies ranges::borrowed_range oris_const_v<element_type> is true.

  • (16.3)

    remove_cvref_t is not a specialization of span.

  • (16.4)

    remove_cvref_t is not a specialization of array.

  • (16.5)

    is_array_v<remove_cvref_t> is false.

  • (16.6)

    is_convertible_v<U()[], element_type()[]> is true. [Note 5: The intent is to allow only qualification conversions of the range reference type to element_type. — end note]

17

#

Preconditions:

18

#

Hardened preconditions: If extent is not equal to dynamic_extent, then ranges::size(r) == extent is true.

19

#

Effects: Initializes data_ with ranges::data(r) andsize_ with ranges::size(r).

20

#

Throws: What and when ranges::data(r) and ranges::size(r) throw.

🔗

constexpr explicit(extent != dynamic_extent) span(std::initializer_list<value_type> il);

21

#

Constraints: is_const_v<element_type> is true.

22

#

Hardened preconditions: If extent is not equal to dynamic_extent, then il.size() == extent is true.

23

#

Effects: Initializes data_ with il.begin() andsize_ with il.size().

🔗

constexpr span(const span& other) noexcept = default;

24

#

Postconditions: other.size() == size() && other.data() == data().

🔗

template<class OtherElementType, size_t OtherExtent> constexpr explicit(see below) span(const span<OtherElementType, OtherExtent>& s) noexcept;

25

#

Constraints:

extent == dynamic_extent || OtherExtent == dynamic_extent || extent == OtherExtent is true, and

is_convertible_v<OtherElementType()[], element_type()[]> is true. [Note 6: The intent is to allow only qualification conversions of the OtherElementType to element_type. — end note]

26

#

Hardened preconditions: If extent is not equal to dynamic_extent, then s.size() == extent is true.

27

#

Effects: Constructs a span that is a view over the range [s.data(), s.data() + s.size()).

28

#

Postconditions: size() == s.size() && data() == s.data().

29

#

Remarks: The expression inside explicit is equivalent to:extent != dynamic_extent && OtherExtent == dynamic_extent

🔗

constexpr span& operator=(const span& other) noexcept = default;

30

#

Postconditions: size() == other.size() && data() == other.data().

23.7.2.2.3 Deduction guides [span.deduct]

🔗

template<class It, class EndOrSize> span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<It>>, [maybe-static-ext](span.syn#concept:maybe-static-ext "23.7.2.1Header <span> synopsis[span.syn]")<EndOrSize>>;

1

#

Constraints: It satisfies contiguous_iterator.

🔗

template<class R> span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;

2

#

Constraints: R satisfies ranges::contiguous_range.

23.7.2.2.4 Subviews [span.sub]

🔗

template<size_t Count> constexpr span<element_type, Count> first() const;

1

#

Mandates: Count <= Extent is true.

2

#

Hardened preconditions: Count <= size() is true.

3

#

Effects: Equivalent to: return R{data(), Count}; where R is the return type.

🔗

template<size_t Count> constexpr span<element_type, Count> last() const;

4

#

Mandates: Count <= Extent is true.

5

#

Hardened preconditions: Count <= size() is true.

6

#

Effects: Equivalent to: return R{data() + (size() - Count), Count}; where R is the return type.

🔗

template<size_t Offset, size_t Count = dynamic_extent> constexpr span<element_type, see below> subspan() const;

7

#

Mandates: Offset <= Extent && (Count == dynamic_extent || Count <= Extent - Offset) is true.

8

#

Hardened preconditions: Offset <= size() && (Count == dynamic_extent || Count <= size() - Offset) is true.

9

#

Effects: Equivalent to:return span<ElementType, see below>( data() + Offset, Count != dynamic_extent ? Count : size() - Offset);

10

#

Remarks: The second template argument of the returned span type is:Count != dynamic_extent ? Count : (Extent != dynamic_extent ? Extent - Offset : dynamic_extent)

🔗

constexpr span<element_type, dynamic_extent> first(size_type count) const;

11

#

Hardened preconditions: count <= size() is true.

12

#

Effects: Equivalent to: return {data(), count};

🔗

constexpr span<element_type, dynamic_extent> last(size_type count) const;

13

#

Hardened preconditions: count <= size() is true.

14

#

Effects: Equivalent to: return {data() + (size() - count), count};

🔗

constexpr span<element_type, dynamic_extent> subspan( size_type offset, size_type count = dynamic_extent) const;

15

#

Hardened preconditions: offset <= size() && (count == dynamic_extent || count <= size() - offset) is true.

16

#

Effects: Equivalent to:return {data() + offset, count == dynamic_extent ? size() - offset : count};

23.7.2.2.5 Observers [span.obs]

🔗

constexpr size_type size() const noexcept;

1

#

Effects: Equivalent to: return size_;

🔗

constexpr size_type size_bytes() const noexcept;

2

#

Effects: Equivalent to: return size() * sizeof(element_type);

🔗

constexpr bool empty() const noexcept;

3

#

Effects: Equivalent to: return size() == 0;

23.7.2.2.6 Element access [span.elem]

🔗

constexpr reference operator[](size_type idx) const;

1

#

Hardened preconditions: idx < size() is true.

2

#

Returns: *(data() + idx).

3

#

Throws: Nothing.

🔗

constexpr reference at(size_type idx) const;

4

#

Returns: *(data() + idx).

5

#

Throws: out_of_range if idx >= size() is true.

🔗

constexpr reference front() const;

6

#

Hardened preconditions: empty() is false.

7

#

Returns: *data().

8

#

Throws: Nothing.

🔗

constexpr reference back() const;

9

#

Hardened preconditions: empty() is false.

10

#

Returns: *(data() + (size() - 1)).

11

#

Throws: Nothing.

🔗

constexpr pointer data() const noexcept;

12

#

Returns: data_.

23.7.2.2.7 Iterator support [span.iterators]

🔗

using iterator = implementation-defined;

1

#

The type models contiguous_iterator ([iterator.concept.contiguous]), meets the Cpp17RandomAccessIterator requirements ([random.access.iterators]), and meets the requirements for constexpr iterators ([iterator.requirements.general]), whose value type is value_type and whose reference type is reference.

2

#

All requirements on container iterators ([container.reqmts]) apply tospan::iterator as well.

🔗

constexpr iterator begin() const noexcept;

3

#

Returns: An iterator referring to the first element in the span.

If empty() is true, then it returns the same value as end().

🔗

constexpr iterator end() const noexcept;

4

#

Returns: An iterator which is the past-the-end value.

🔗

constexpr reverse_iterator rbegin() const noexcept;

5

#

Effects: Equivalent to: return reverse_iterator(end());

🔗

constexpr reverse_iterator rend() const noexcept;

6

#

Effects: Equivalent to: return reverse_iterator(begin());