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

From cppreference.com
< cpp‎ | container‎ | span
constexpr span() noexcept;
(1)
template <class It>
constexpr span(It first, size_type count);
(2)
template <class It, class End>
constexpr span(It first, End last);
(3)
template <std::size_t N>
constexpr span(element_type (&arr)[N]) noexcept;
(4)
template <std::size_t N>
constexpr span(std::array<value_type, N>& arr) noexcept;
(5)
template <std::size_t N>
constexpr span(const std::array<value_type, N>& arr) noexcept;
(6)
template <class R>
constexpr span(R&& r);
(7)
template <class U, std::size_t N>
constexpr span(const std::span<U, N>& s) 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 only participates in overload resolution 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. The behavior is undefined if [first, first + count) is not a valid range, if It does not actually model contiguous_iterator, or if extent != std::dynamic_extent && count != extent. These overloads only participate in overload resolution if
  • It satisfies contiguous_iterator
  • the conversion from std::iter_reference_t<It> to element_type is at most a qualification conversion.
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. The behavior is undefined if [first, last) is not a valid range, if It does not actually model contiguous_iterator, if End does not actually model sized_sentinel_for for It, or if extent != std::dynamic_extent && last-first != extent. These overloads only participate in overload resolution if
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 only participate in overload resolution if extent == std::dynamic_extent || N == extent is true and std::remove_pointer_t<decltype(std::data(arr))>(*)[] is convertible to element_type (*)[]
7) Constructs a span that is a view over the range r; the resulting span has size() == std::ranges::size(r) and data() == std::ranges::data(r).

The behavior is undefined if R does not actually model contiguous_range and sized_range or if R does not model borrowed_range while element_type is non-const. These overloads only participate in overload resolution if

  • extent == dynamic_extent;
  • R satisfies contiguous_range and sized_range,
  • either R satisfies borrowed_range or std::is_const_v<element_type> is true
  • std::remove_cvref_t<R> is not a specialization of std::span,
  • std::remove_cvref_t<R> is not a specialization of std::array
  • std::is_array_v<std::remove_cvref_t<R>> is false, and
  • the conversion from std::ranges::range_reference_t<R> to element_type is at most a qualification conversion
8) Converting constructor from another span s; the resulting span has size() == s.size() and data() == s.data(). This overload only participates in overload resolution if extent == std::dynamic_extent || N == extent is true and U (*)[] is convertible to element_type (*)[].
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
r - range to construct a view for
s - another span to convert from
other - another span to copy from

Exceptions

2-3) Throws what and when std::to_address(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)