std::span<T,Extent>:: span
|
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 ( const std:: 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 a
span
.
Contents |
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 |
| r | - | range to construct a view for |
| source | - |
another
span
to convert from
|
| other | - |
another
span
to copy from
|
Effects
| 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 ( ) |
Constraints and supplement information
Size requirements
If
extent
is not
std::
dynamic_extent
and the size of the source range is different from
extent
, the
span
object cannot be constructed.
These overloads participate in overload resolution only if the result of the following expression is true :
|
If the result of the following expression is false , the behavior is undefined. |
(until C++26) |
|
If the result of the following expression is false :
|
(since C++26) |
Conversion requirements
If
element_type
is different from the element type of the source range, and the latter cannot be converted to the former by
qualification conversion
, the
span
object cannot be constructed.
These overloads participate in overload resolution only if
std::
is_convertible_v
<
U
(
*
)
[
]
, element_type
(
*
)
[
]
>
is
true
, where
U
is defined as follows:
U
Concept requirements
If any template argument does not model certain concept(s), the
span
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 if std:: is_const_v < element_type > is false |
Other constraints
[
first
,
last
)
is not a valid range, the behavior is undefined.
-
std::
remove_cvref_t
<
R
>
is not a specialization of
std::spanor std::array . - std:: is_array_v < std:: remove_cvref_t < R >> is false .
Exceptions
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) |