(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member functions | ||||
span::span | ||||
| Element access | ||||
(C++26) | ||||
| Iterators | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Observers | ||||
| Subviews | ||||
| Non-member functions | ||||
| Non-member constant | ||||
| Deduction guides |
constexpr span()noexcept; | (1) | (since C++20) |
template<class It> explicit(extent!=std::dynamic_extent) | (2) | (since C++20) |
template<class It,class End> explicit(extent!=std::dynamic_extent) | (3) | (since C++20) |
template<std::size_t N> constexpr span(std::type_identity_t<element_type>(&arr)[N])noexcept; | (4) | (since C++20) |
template<class U,std::size_t N> constexpr span(std::array<U, N>& arr)noexcept; | (5) | (since C++20) |
template<class U,std::size_t N> constexpr span(conststd::array<U, N>& arr)noexcept; | (6) | (since C++20) |
template<class R> explicit(extent!=std::dynamic_extent) | (7) | (since C++20) |
explicit(extent!=std::dynamic_extent) constexpr span(std::initializer_list<value_type> il)noexcept; | (8) | (since C++26) |
template<class U,std::size_t N> explicit(extent!=std::dynamic_extent&& N==std::dynamic_extent) | (9) | (since C++20) |
constexpr span(const span& other)noexcept=default; | (10) | (since C++20) |
Constructs aspan.
Contents |
| first | - | iterator to the first element of the sequence |
| count | - | number of elements in the sequence |
| last | - | iterator past the last element of the sequence or another sentinel |
| arr | - | array to construct a view for |
| r | - | range to construct a view for |
| source | - | anotherspan to convert from |
| other | - | anotherspan to copy from |
| Overload | data() after construction | size() after construction |
|---|---|---|
| (1) | nullptr | 0 |
| (2) | std::to_address(first) | count |
| (3) | last- first | |
| (4) | std::data(arr) | N |
| (5) | ||
| (6) | ||
| (7) | ranges::data(r) | ranges::size(r) |
| (8) | il.begin() | il.size() |
| (9) | source.data() | source.size() |
| (10) | other.data() | other.size() |
Ifextent is notstd::dynamic_extent and the size of the source range is different fromextent, thespan object cannot be constructed.
These overloads participate in overload resolution only if the result of the following expression istrue:
If the result of the following expression isfalse, the behavior is undefined. | (until C++26) |
If the result of the following expression isfalse:
| (since C++26) |
Ifelement_type is different from the element type of the source range, and the latter cannot be converted to the former byqualification conversion, thespan object cannot be constructed.
These overloads participate in overload resolution only ifstd::is_convertible_v<U(*)[], element_type(*)[]> istrue, whereU is defined as follows:
UIf any template argument does not model certain concept(s), thespan object cannot be constructed.
These overloads participate in overload resolution only if the template argument corresponding to the specified template parameter satisfies the corresponding concept(s). If it does not meet the semantic requirements of any corresponding concept, the behavior is undefined:
| Overload | Template parameter | Concept | Remark |
|---|---|---|---|
| (2) | It | contiguous_iterator | |
| (3) | It | contiguous_iterator | |
End | sized_sentinel_for<It> | ||
| (7) | R | contiguous_range | |
sized_range | |||
borrowed_range | only required ifstd::is_const_v<element_type> isfalse |
[first, last) is not a valid range, the behavior is undefined.std::span orstd::array.| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_span_initializer_list | 202311L | (C++26) | Constructingstd::span from astd::initializer_list,(8) |
#include <array>#include <iostream>#include <span>#include <vector> void print_span(std::span<constint> s){for(int n: s)std::cout<< n<<' ';std::cout<<'\n';} int main(){int c[]{1,2,3}; print_span(c);// constructs from array std::array a{4,5,6}; print_span(a);// constructs from std::array std::vector v{7,8,9}; print_span(v);// constructs from std::vector #if __cpp_lib_span_initializer_list print_span({0,1,2});// constructs from initializer_list#else print_span({{0,1,2}});// ditto, a workaround#endif}
Output:
1 2 3 4 5 67 8 90 1 2
| direct access to the underlying contiguous storage (public member function)[edit] | |
| returns the number of elements (public member function)[edit] | |
assigns aspan(public member function)[edit] | |
(C++17)(C++20) | returns the size of a container or array (function template)[edit] |
(C++17) | obtains the pointer to the underlying array (function template)[edit] |