std::span::span(3)
welcome to std::span::span(3) from the C++ Standard Libary β yes bestie, that's the manual name, and the title is literally std::span::span π
β¨.
πΈ NAME πΈ
std::span::span β std::span::span (that's the whole name section, and honestly? iconic.)
β¨ Synopsis β¨
the overload lineup, no cap:
constexpr span() noexcept; (1)
(since C++20)
template< class It >
explicit(extent != std::dynamic_extent)
constexpr span( It first, size_type count ); (2)
(since C++20)
template< class It, class End >
explicit(extent != std::dynamic_extent)
constexpr span( It first, End last ); (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 > (5)
(since C++20)
constexpr span( std::array<U, N>& arr ) noexcept;
template< class U, std::size_t N > (6)
(since C++20)
constexpr span( const std::array<U, N>& arr ) noexcept;
template< class R >
explicit(extent != std::dynamic_extent)
constexpr span( R&& range ); (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)
constexpr span( const std::span<U, N>& source ) noexcept; (9)
(since C++20)
constexpr span( const span& other ) noexcept = default;
(10) (since C++20)
constructs a span. like, that's the whole gig π«Ά.
π«Ά overload-by-overload tea π«Ά
(1)β constructs an empty span wheredata() == nullptrandsize() == 0. she participates in overload resolution only ifextent == 0 || extent == std::dynamic_extent.(2)β constructs a span that is a view over the range[first, first + count); the resulting span hasdata() == std::to_address(first)andsize() == count.- > π¨ UB alert: the behavior is undefined if
[first, first + count)is not a valid range, ifItdoes not actually modelcontiguous_iterator, or ifextent != std::dynamic_extent && count != extent. - this overload participates in overload resolution only if:
Itsatisfiescontiguous_iterator,- the conversion from
std::iter_reference_t<It>toelement_typeis at most a qualification conversion.
- > π¨ UB alert: the behavior is undefined if
(3)β constructs a span that is a view over the range[first, last); the resulting span hasdata() == std::to_address(first)andsize() == last-first.- > π¨ UB alert: the behavior is undefined if
[first, last)is not a valid range, ifItdoes not actually modelcontiguous_iterator, ifEnddoes not actually modelsized_sentinel_forforIt, or ifextent != std::dynamic_extent && last-first != extent. - this overload participates in overload resolution only if:
Itsatisfiescontiguous_iterator,Endsatisfiessized_sentinel_forforIt,- the conversion from
std::iter_reference_t<It>toelement_typeis at most a qualification conversion, and std::is_convertible_v<End, std::size_t>is false.
- > π¨ UB alert: the behavior is undefined if
4-6)β constructs a span that is a view over the arrayarr; the resulting span hassize() == Nanddata() == std::data(arr).- these overloads participate in overload resolution only if
extent == std::dynamic_extent || N == extentis true and the conversion fromstd::remove_pointer_t<decltype(data(arr))>toelement_typeis at most a qualification conversion.
- these overloads participate in overload resolution only if
(7)β constructs a span that is a view over the rangerange; the resulting span hassize() == std::ranges::size(range)anddata() == std::ranges::data(range).- > π¨ UB alert: the behavior is undefined if
Rdoes not actually modelcontiguous_rangeandsized_range, or ifRdoes not modelborrowed_rangewhileelement_typeis non-const, or bothextent != dynamic_extentandstd::ranges::size(range) != extentare true. - this overload participates in overload resolution only if:
Rsatisfiescontiguous_rangeandsized_range,- either
Rsatisfiesborrowed_rangeorstd::is_const_v<element_type>is true, 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_typeis at most a qualification conversion.
- > π¨ UB alert: the behavior is undefined if
(8)β constructs a span that is a view over the initializer listil; the resulting span hassize() == il.size()anddata() == il.begin().- > π¨ UB alert: the behavior is undefined if both
extent != dynamic_extentandil.size() != extentare true. - this overload participates in overload resolution only if
std::is_const_v<element_type>is true.
- > π¨ UB alert: the behavior is undefined if both
(9)β converting constructor from another spansource; the resulting span hassize() == source.size()anddata() == source.data().- > π¨ UB alert: the behavior is undefined if both
extent != dynamic_extentandsource.size() != extentare true. - this overload participates in overload resolution only if at least one of
extent == std::dynamic_extent,N == std::dynamic_extentandN == extentis true and the conversion fromUtoelement_typeis at most a qualification conversion.
- > π¨ UB alert: the behavior is undefined if both
(10)β defaulted copy constructor copies the size and data pointer; the resulting span hassize() == other.size()anddata() == other.data().
π Parameters π
firstβ iterator to the first element of the sequencecountβ number of elements in the sequencelastβ iterator past the last element of the sequence or another sentinelarrβ array to construct a view forrangeβ range to construct a view forsourceβ another span to convert fromotherβ another span to copy from
π Exceptions π
2)throws nothing.3)throws what and whenlast - firstthrows.7)throws what and whenstd::ranges::size(r)andstd::ranges::data(r)throw.
π§ Notes π§
feature-test macro check, bestie:
__cpp_lib_span_initializer_listβ value202311Lβ(C++26)β feature: Constructingstd::spanfrom astd::initializer_list,(8).
π Example π
// Run this code
#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 π
dataβ direct access to the underlying contiguous storage β(public member function)sizeβ returns the number of elements β(public member function)operator=β assigns a span β(public member function)size,ssizeβ returns the size of a container or array β(C++17),(C++20)β(function template)dataβ obtains the pointer to the underlying array β(C++17)β(function template)
okay that's the page! cute sign-off time: source/version/date is http://cppreference.com, 2024.06.10, and it's all for std::span::span(3) β mwah πππ¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms