std::span
| <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 at compile-time and encoded in the type, or a dynamic extent.
If a span has dynamic extent a typical implementation holds two members: a pointer to T and a size.
A span with static extent may have only one member: a pointer to T.
|
Every specialization of |
(since C++23) |
Template parameters
| T | - | element type; must be a complete object 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>
|
size_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 contiguous_iterator whose value_type is value_type
|
reverse_iterator
|
std::reverse_iterator<iterator>
|
Note: iterator is a mutable iterator if T is not const-qualified.
All requirements on the iterator types of a Container apply to the iterator type of span as well.
Member constant
<tbody> </tbody> static constexpr std::size_t extent = Extent; |
||
Member functions
span 을 생성합니다 (public member function) | |
span을 할당합니다 (public member function) | |
Iterators | |
| 첫번째 원소로의 반복자(iterator)를 반환한다. (public member function) | |
| 마지막 원소로의 반복자(iterator)를 반환한다. (public member function) | |
| 첫번째 원소로의 역방향 반복자(reverse iterator)를 반환한다. (public member function) | |
| 마지막 원소로의 역방향 반복자(reverse iterator)를 반환한다. (public member function) | |
Element access | |
| 첫번째 요소에 접근한다. (public member function) | |
| 마지막 요소에 접근한다. (public member function) | |
| 연속체의 요소에 접근합니다 (public member function) | |
| 연속체의 시작 지점 포인터를 반환합니다 (public member function) | |
Observers | |
| 연속체 안의 요소 개수를 반환합니다 (public member function) | |
| 연속체의 크기를 바이트(byte) 단위값으로 반환합니다 (public member function) | |
| 연속체가 비어있는지 검사합니다 (public member function) | |
Subviews | |
| 연속체의 첫 N개의 요소를 담은 span을 획득합니다 (public member function) | |
| 연속체의 마지막 N개의 요소를 담은 span 을 획득합니다 (public member function) | |
| span의 하위span을 획득합니다 (public member function) | |
Non-member functions
(C++20) |
span 을 하위 byte데이터에 대한 보기(view)로 변환합니다 (function template) |
Non-member constant
(C++20) |
span이 동적 크기임을 나타내는 size_t 타입의 상수 (constant) |
Helper templates
<tbody> </tbody> template<class T, std::size_t Extent> inline constexpr bool ranges::enable_borrowed_range<std::span<T, Extent>> = true; |
||
This specialization of ranges::enable_borrowed_range makes span satisfy borrowed_range.
template<class T, std::size_t Extent> inline constexpr bool ranges::enable_view<std::span<T, Extent>> = true; |
||
This specialization of ranges::enable_view makes span satisfy view.
Deduction guides
Notes
Specializations of std::span are already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23.
Example
The example uses std::span to implement some algorithms on contiguous ranges.
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <span>
template<class T, std::size_t N> [[nodiscard]]
constexpr auto slide(std::span<T,N> s, std::size_t offset, std::size_t width) {
return s.subspan(offset, offset + width <= s.size() ? width : 0U);
}
template<class T, std::size_t N, std::size_t M> [[nodiscard]]
constexpr bool starts_with(std::span<T,N> data, std::span<T,M> prefix) {
return data.size() >= prefix.size()
&& std::equal(prefix.begin(), prefix.end(), data.begin());
}
template<class T, std::size_t N, std::size_t M> [[nodiscard]]
constexpr bool ends_with(std::span<T,N> data, std::span<T,M> suffix) {
return data.size() >= suffix.size()
&& std::equal(data.end() - suffix.size(), data.end(),
suffix.end() - suffix.size());
}
template<class T, std::size_t N, std::size_t M> [[nodiscard]]
constexpr bool contains(std::span<T,N> span, std::span<T,M> sub) {
return std::search(span.begin(), span.end(), sub.begin(), sub.end()) != span.end();
// return std::ranges::search(span, sub).begin() != span.end();
}
void print(const auto& seq) {
for (const auto& elem : seq) std::cout << elem << ' ';
std::cout << '\n';
}
int main()
{
constexpr int a[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
constexpr int b[] { 8, 7, 6 };
for (std::size_t offset{}; ; ++offset) {
constexpr std::size_t width{6};
auto s = slide(std::span{a}, offset, width);
if (s.empty())
break;
print(s);
}
static_assert(starts_with(std::span{a}, std::span{a,4})
&& starts_with(std::span{a+1, 4}, std::span{a+1,3})
&& !starts_with(std::span{a}, std::span{b})
&& !starts_with(std::span{a,8}, std::span{a+1,3})
&& ends_with(std::span{a}, std::span{a+6,3})
&& !ends_with(std::span{a}, std::span{a+6,2})
&& contains(std::span{a}, std::span{a+1,4})
&& !contains(std::span{a,8}, std::span{a,9}));
}
Output:
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P2325R3 | C++20 | span of non-zero static extents were not view
|
they are as default_initializable is not required
|
See also
(C++11) |
allows the use of initializer list syntax to initialize non plain-old-data types (class template) |
(C++17) |
읽기 전용의 문자열 보기(view) (class template) |
(C++20) |
combines an iterator-sentinel pair into a view (class template) |