cpp / latest / container / span / span.html /

std::span<T,Extent>::span

constexpr span() noexcept;
(1)
template< class It >
explicit(extent != std::dynamic_extent)
constexpr span( It first, size_type count );
(2)
template< class It, class End >
explicit(extent != std::dynamic_extent)
constexpr span( It first, End last );
(3)
template< std::size_t N >
constexpr span( element_type (&arr)[N] ) noexcept;
(4)
template< class U, std::size_t N >
constexpr span( std::array<U, N>& arr ) noexcept;
(5)
template< class U, std::size_t N >
constexpr span( const std::array<U, N>& arr ) noexcept;
(6)
template< class R >
explicit(extent != std::dynamic_extent)
constexpr span( R&& range );
(7)
template< class U, std::size_t N >
explicit(extent != std::dynamic_extent && N == std::dynamic_extent)
constexpr span( const std::span<U, N>& source ) noexcept;
(8)
constexpr span( const span& other ) noexcept = default;
(9)

Constructs a span.

1) Constructs an empty span whose data() == nullptr and size() == 0.
  • This overload participates in overload resolution only if extent == 0 || extent == std::dynamic_extent.
2) Constructs a span that is a view over the range [first, first + count); the resulting span has data() == std::to_address(first) and size() == count. 3) Constructs a span that is a view over the range [first, last); the resulting span has data() == std::to_address(first) and size() == last-first. 4-6) Constructs a span that is a view over the array 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 is true and the conversion from std::remove_pointer_t<decltype(data(arr))> to element_type is at most a qualification conversion. These constructor templates are never used for class template argument deduction.
7) Constructs a span that is a view over the range range; the resulting span has size() == std::ranges::size(range) and data() == std::ranges::data(range). 8) Converting constructor from another span source; the resulting span has size() == source.size() and data() == source.data().
  • The behavior is undefined if both extent != dynamic_extent and source.size() != extent are true.
  • This overload participates in overload resolution only if at least one of extent == std::dynamic_extent, N == std::dynamic_extent and N == extent is true and the conversion from U to element_type is at most a qualification conversion.
9) 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.
7) Throws what and when std::ranges::size(r) and std::ranges::data(r) throw.

See also

returns a pointer to the beginning of the sequence of elements
(public member function)
returns the number of elements in the sequence
(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