# std::span

From cppreference.com
< cpp‎ | container

Defined in header `<span>` |  |
template< class T, std::size_t Extent = std::dynamic_extent > class span; |  | (since C++20)

The class template `span` describes an object that can refer to a contiguous sequence of objects with the first element of the sequence at position zero. A `span` can either have a static extent, in which case the number of elements in the sequence is known and encoded in the type, or a dynamic extent.

A typical implementation holds only two members: a pointer to `T` and a size.

### Template parameters

T | - | element type; must be a complete type that is not an abstract class type
Extent | - | the number of elements in the sequence, or `std::dynamic_extent` if dynamic

### Member types

Member type | Definition
`element_type` | `T`
`value_type` | std::remove_cv_t<T>
`index_type` | std::size_t
`difference_type` | std::ptrdiff_t
`pointer` | `T*`
`const_pointer` | `const T*`
`reference` | `T&`
`const_reference` | `const T&`
`iterator` | implementation-defined LegacyRandomAccessIterator, ConstexprIterator, and LegacyContiguousIterator whose `value_type` is `value_type`
`const_iterator` | implementation-defined constant LegacyRandomAccessIterator, ConstexprIterator, and LegacyContiguousIterator whose `value_type` is `value_type`
`reverse_iterator` | std::reverse_iterator<iterator>
`const_reverse_iterator` | std::reverse_iterator<const_iterator>

Note: `iterator` is a mutable iterator if `T` is not const-qualified.

All requirements on the iterator types of a Container applies to the `iterator` and `const_iterator` types of `span` as well.

### Member constant

static constexpr std::size_t extent = Extent; |  |

### Member functions

(constructor) | constructs a `span` (public member function)
operator= | assigns a `span` (public member function)

##### Iterators

begin cbegin | returns an iterator to the beginning of the sequence (public member function)
end cend | returns an iterator to the end of the sequence (public member function)
rbegin crbegin | returns a reverse iterator to the beginning of the sequence (public member function)
rend crend | returns a reverse iterator to the end of the sequence (public member function)

##### Element access

front | access the first element (public member function)
back | access the last element (public member function)
operator[] | accesses an element of the sequence (public member function)
data | returns a pointer to the beginning of the sequence of elements (public member function)

##### Observers

size | returns the number of elements in the sequence (public member function)
size_bytes | returns the size of the sequence in bytes (public member function)
empty | checks if the sequence is empty (public member function)

##### Subviews

first | obtains a subspan consisting of the first N elements of the sequence (public member function)
last | obtains a subspan consisting of the last N elements of the sequence (public member function)
subspan | obtains a subspan (public member function)

### Non-member functions

begin end | returns iterators to the beginning and the end of the span (function)
as_bytes as_writable_bytes | converts a `span` into a view of its underlying bytes (function template)
std::get(std::span) | accesses an element of a static-extent `span` (function template)

### Non-member constant

dynamic_extent (C++20) | a constant of type `size_t` signifying that the `span` has dynamic extent (constant)

### Helper classes

std::tuple_size<std::span> | obtains the size of a static-extent `span` (class template specialization)
std::tuple_element<std::span> | obtains the type of the elements of a static-extent `span` (class template specialization)

### Deduction guides

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/span&oldid=110376"