std::span constructors
std::span
class can be constructed in many different ways.
Use the button in the top-right corner to navigate with arrows for convenience.
std::span
is a class template, with following type parameters,
that are used within constructors:
pub | T | Type of the elements. |
pub | Extent | Number of elements in the container or |
Default constructor
- C++20
- Simplified
- Detailed
span();
- this constructor is
constexpr
andnoexcept
constexpr span() noexcept;
Default constructor. Constructs an empty span with
data() == nullptr
size() == 0
Overload resolution
This overload participates in overload resolution only if
extent == 0
or
extent == std::dynamic_extent
Complexity
Constant - O(1).
Example
#include <iostream>
#include <span>
int main() {
// Default constructed span<int>:
std::span<int> s;
std::cout << "s.size(): " << s.size();
}
s.size(): 0
Iterator + size constructor
- C++20
template< class It >
explicit(extent != std::dynamic_extent)
constexpr span( It first, size_type count );
Constructs a span that is a view over the range [ first, first + count ).
The resulting span has:
data() == std::to_address(first)
size() == count
The behavior is undefined
if:- [ first, first + count ) is not a valid range
It
does not modelcontiguous_iterator
orextent != std::dynamic_extent
andcount != extent
Overload resolution
This overload participates in overload resolution only if
It
satisfiescontiguous_iterator
and- the conversion from
std::iter_reference_t<It>
toelement_type
is at most a qualification conversion
Complexity
Constant - O(1).
Example
#include <iostream>
#include <vector>
#include <span>
int main() {
std::vector<int> v{1, 2, 3, 4, 5};
std::span<int> s(v.cbegin(), 3);
std::cout << "s.size(): " << s.size() << '\n';
std::cout << "elems: ";
for(int num : s) std::cout << num << ' ';
}
s.size(): 3
elems: 1 2 3
Range based constructor (iterators)
- C++20
template< class It, class End >
explicit(extent != std::dynamic_extent)
constexpr span( It first, End last );
onstructs a span that is a view over the range [ first, last ).
The resulting span has:
data() == std::to_address(first)
size() == last - first
The behavior is undefined
if:- [ first, first ) is not a valid range
It
does not modelcontiguous_iterator
End
does not modelsized_sentinel_for
It
orextent != std::dynamic_extent
andlast - first != extent
Overload resolution
This overload participates in overload resolution only if
It
satisfiescontiguous_iterator
End
satisfiessized_sentinel_for
- 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
Complexity
Constant - O(1).
Example
#include <iostream>
#include <vector>
#include <span>
int main() {
std::vector<int> v{1, 2, 3, 4, 5};
std::span<int> s(v.begin() + 1, v.end() - 1);
std::cout << "s.size(): " << s.size() << '\n';
std::cout << "elems: ";
for(int num : s) {
std::cout << num << ' ';
}
}
s.size(): 3
elems: 2 3 4
C array and std::array
constructors
- C++20
// (1)
template< std::size_t N >
constexpr span( std::type_identity_t<element_type> (&arr)[N] ) noexcept;
// (2)
template< class U, std::size_t N >
constexpr span( std::array<U, N>& arr ) noexcept;
// (3)
template< class U, std::size_t N >
constexpr span( const std::array<U, N>& arr ) noexcept;
Constructs a span that is a view over the array arr
.
The resulting span has:
size() == N
data() == std::data(arr)
Overload resolution
These overloads participate in overload resolution only if:
extent == std::dynamic_extent
orN == extent
and- the conversion from
std::remove_pointer_t<decltype(data(arr))>
toelement_type
is at most a qualification conversion
Complexity
Constant - O(1).
Example
#include <iostream>
#include <array>
#include <span>
template <typename Type, std::size_t Extent>
void present_span(std::span<Type, Extent> s) {
std::cout << "s.size(): " << s.size() << '\n';
std::cout << "elems: ";
for(int num : s) {
std::cout << num << ' ';
}
std::cout << "\n\n";
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
present_span(std::span(arr)); // (1)
std::array<int, 5> arr2 = {6, 7, 8, 9, 10};
present_span(std::span(arr2)); // (2)
const std::array<int, 5> arr3 = {12, 13, 14, 15, 16};
present_span(std::span(arr3)); // (3)
}
s.size(): 5
elems: 1 2 3 4 5
s.size(): 5
elems: 6 7 8 9 10
s.size(): 5
elems: 12 13 14 15 16
Arbitrary range constructor
- C++20
template< class R >
explicit(extent != std::dynamic_extent)
constexpr span( R&& range );
Constructs a span that is a view over the range range
.
The resulting span has:
size() == std::ranges::size(range)
data() == std::ranges::data(range)
The behavior is undefined
if:R
does not modelcontiguous_range
andsized_range
orR
does not modelborrowed_range
, while:element_type
is non-const
orextent != std::dynamic_extent
andstd::ranges::size(range) != extent
Overload resolution
These overloads participate 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>>
isfalse
and- the conversion from
std::ranges::range_reference_t<R>
toelement_type
is at most a qualification conversion
Complexity
Constant - O(1).
Example
#include <iostream>
#include <vector>
#include <span>
template <typename Type, std::size_t Extent>
void present_span(std::span<Type, Extent> s) {
std::cout << "s.size(): " << s.size() << '\n';
std::cout << "elems: ";
for(int num : s) {
std::cout << num << ' ';
}
std::cout << "\n\n";
}
int main() {
std::vector<int> v = {1, 2, 3, 4};
present_span(std::span(v)); // vector is a valid range
struct custom_range {
int* begin() { return arr.data(); }
int* end() { return arr.data() + arr.size(); }
std::vector<int> arr{1, 2, 3};
};
custom_range cr;
present_span(std::span(cr)); // cr is a valid range
}
s.size(): 4
elems: 1 2 3 4
s.size(): 3
elems: 1 2 3
Converting constructor
- C++20
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;
Converting constructor from another span source. The resulting span has:
size() == source.size()
data() == source.data()
The behavior is undefined
if:extent != dynamic_extent
andsource.size() != extent
Overload resolution
These overloads participate in overload resolution only if:
- at least one of:
extent == std::dynamic_extent
N == std::dynamic_extent
N == extent
istrue
and
- the conversion from
U
toelement_type
is at most a qualification conversion
Complexity
Constant - O(1).
Example
#include <iostream>
#include <vector>
#include <span>
template <typename Type, std::size_t Extent>
void present_span(std::span<Type, Extent> s) {
std::cout << "s.size(): " << s.size() << '\n';
std::cout << "elems: ";
for(int num : s) {
std::cout << num << ' ';
}
std::cout << "\n\n";
}
int main() {
std::vector<int> source = { 1, 2, 3, 4, 5 };
std::array<const int, 4> source2 = { 10, 11, 12, 13 };
std::span<int> s1 = source;
std::span<const int> s2 = source2;
present_span(s1);
present_span(s2);
s2 = s1; // converting ctor
present_span(s1);
present_span(s2);
}
s.size(): 5
elems: 1 2 3 4 5
s.size(): 4
elems: 10 11 12 13
s.size(): 5
elems: 1 2 3 4 5
s.size(): 5
elems: 1 2 3 4 5
Defaulted copy constructor
- C++20
constexpr span( const span& other ) noexcept = default;
Defaulted copy constructor copies the size and data pointer The resulting span has:
size() == other.size()
data() == other.data()
Complexity
Constant - O(1).
Example
#include <iostream>
#include <vector>
#include <span>
template <typename Type, std::size_t Extent>
void present_span(std::span<Type, Extent> s) {
std::cout << "s.size(): " << s.size() << '\n';
std::cout << "elems: ";
for(int num : s) {
std::cout << num << ' ';
}
std::cout << "\n\n";
}
int main() {
std::vector<int> source = { 1, 2, 3, 4, 5 };
std::vector<int> source2 = { 5, 4, 3, 2, 1 };
std::span<int> s1 = source;
std::span<int> s2 = source2;
present_span(s1);
present_span(s2);
s2 = s1; // copy ctor
present_span(s1);
present_span(s2);
}
s.size(): 5
elems: 1 2 3 4 5
s.size(): 5
elems: 5 4 3 2 1
s.size(): 5
elems: 1 2 3 4 5
s.size(): 5
elems: 1 2 3 4 5
Hover to see the original license.