On this page
std::span<T,Extent>::span
|
(1) | (since C++20) |
|
(2) | (since C++20) |
|
(3) | (since C++20) |
|
(4) | (since C++20) |
|
(5) | (since C++20) |
|
(6) | (since C++20) |
|
(7) | (since C++20) |
|
(8) | (since C++26) |
|
(9) | (since C++20) |
|
(10) | (since C++20) |
Constructs a span
.
data() == nullptr
and size() == 0
.
- This overload participates in overload resolution only if
extent == 0 || extent == std::dynamic_extent
.
[
first
,
first + count
)
; the resulting span has data() == std::to_address(first)
and size() == count
.
- The behavior is undefined if
[
first
,
first + count
)
is not a valid range, ifIt
does not actually modelcontiguous_iterator
, or ifextent != std::dynamic_extent && count != extent
. - This overload participates in overload resolution only if
It
satisfiescontiguous_iterator
,- the conversion from
std::iter_reference_t<It>
toelement_type
is at most a qualification conversion.
[
first
,
last
)
; the resulting span has data() == std::to_address(first)
and size() == last-first
.
- The behavior is undefined if
[
first
,
last
)
is not a valid range, ifIt
does not actually modelcontiguous_iterator
, ifEnd
does not actually modelsized_sentinel_for
forIt
, or ifextent != std::dynamic_extent && last-first != extent
. - This overload participates in overload resolution only if
It
satisfiescontiguous_iterator
,End
satisfiessized_sentinel_for
forIt
,- the conversion from
std::iter_reference_t<It>
toelement_type
is at most a qualification conversion, and std::is_convertible_v<End, std::size_t>
isfalse
.
arr
; the resulting span has size() == N
and data() == std::data(arr)
.
- These overloads participate in overload resolution only if
extent == std::dynamic_extent || N == extent
istrue
and the conversion fromstd::remove_pointer_t<decltype(data(arr))>
toelement_type
is at most a qualification conversion.
range
; the resulting span has size() == std::ranges::size(range)
and data() == std::ranges::data(range)
.
- The behavior is undefined if
R
does not actually modelcontiguous_range
andsized_range
or ifR
does not modelborrowed_range
whileelement_type
is non-const or bothextent != dynamic_extent
andstd::ranges::size(range) != extent
aretrue
. - This overload participates in overload resolution only if
R
satisfiescontiguous_range
andsized_range
,- either
R
satisfiesborrowed_range
orstd::is_const_v<element_type>
istrue
, std::remove_cvref_t<R>
is not a specialization ofstd::span
,std::remove_cvref_t<R>
is not a specialization ofstd::array
,std::is_array_v<std::remove_cvref_t<R>>
is false, and- the conversion from
std::ranges::range_reference_t<R>
toelement_type
is at most a qualification conversion.
il
; the resulting span has size() == il.size()
and data() == il.begin()
.
- The behavior is undefined if both
extent != dynamic_extent
andil.size() != extent
aretrue
. - This overload participates in overload resolution only if
std::is_const_v<element_type>
istrue
.
source
; the resulting span has size() == source.size()
and data() == source.data()
.
- The behavior is undefined if both
extent != dynamic_extent
andsource.size() != extent
aretrue
. - This overload participates in overload resolution only if at least one of
extent == std::dynamic_extent
,N == std::dynamic_extent
andN == extent
istrue
and the conversion fromU
toelement_type
is at most a qualification conversion.
10) Defaulted copy constructor copies the size and data pointer; the resulting span has
size() == other.size()
and data() == other.data()
.
Parameters
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 |
range | - | range to construct a view for |
source | - | another span to convert from |
other | - | another span to copy from |
Exceptions
2) Throws nothing.
3) Throws what and when
last - first
throws.
Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_span_initializer_list |
202311L | (C++26) | Constructing std::span from a std::initializer_list , (8) |
Example
#include <array>
#include <iostream>
#include <span>
#include <vector>
void print_span(std::span<const int> 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 6
7 8 9
0 1 2
See also
direct access to the underlying contiguous storage (public member function) |
|
returns the number of elements (public member function) |
|
assigns a span (public member function) |
|
(C++17)(C++20)
|
returns the size of a container or array (function template) |
(C++17)
|
obtains the pointer to the underlying array (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/span/span