Przejdź do głównej zawartości

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.

zanotuj

std::span is a class template, with following type parameters, that are used within constructors:

pubT

Type of the elements.

pubExtent

Number of elements in the container or std::dynamic_extent if dynamic.

Default constructor

span();
  • this constructor is constexpr and 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();
}
Result (console)
s.size(): 0

Iterator + size constructor

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
Undefined Behavior

The behavior is undefined

if:

  • [ first, first + count ) is not a valid range
  • It does not model contiguous_iterator
    or
  • extent != std::dynamic_extent and count != extent
Overload resolution

This overload participates in overload resolution only if

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 << ' ';
}
Result (console)
s.size(): 3
elems: 1 2 3

Range based constructor (iterators)

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
Undefined Behavior

The behavior is undefined

if:

Overload resolution

This overload participates in overload resolution only if

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 << ' ';
}
}
Result (console)
s.size(): 3
elems: 2 3 4

C array and std::array constructors

// (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 or N == extent
    and
  • the conversion from std::remove_pointer_t<decltype(data(arr))> to element_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)
}
Result (console)
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

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)
Undefined Behavior

The behavior is undefined

if:

Overload resolution

These overloads participate in overload resolution only if:

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
}
Result (console)
s.size(): 4
elems: 1 2 3 4

s.size(): 3
elems: 1 2 3

Converting constructor

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()
Undefined Behavior

The behavior is undefined

if:

  • extent != dynamic_extent
    and
  • source.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 is true
      and
  • the conversion from U to element_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);
}
Result (console)
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

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);
}
Result (console)
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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

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.

zanotuj

std::span is a class template, with following type parameters, that are used within constructors:

pubT

Type of the elements.

pubExtent

Number of elements in the container or std::dynamic_extent if dynamic.

Default constructor

span();
  • this constructor is constexpr and 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();
}
Result (console)
s.size(): 0

Iterator + size constructor

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
Undefined Behavior

The behavior is undefined

if:

  • [ first, first + count ) is not a valid range
  • It does not model contiguous_iterator
    or
  • extent != std::dynamic_extent and count != extent
Overload resolution

This overload participates in overload resolution only if

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 << ' ';
}
Result (console)
s.size(): 3
elems: 1 2 3

Range based constructor (iterators)

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
Undefined Behavior

The behavior is undefined

if:

Overload resolution

This overload participates in overload resolution only if

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 << ' ';
}
}
Result (console)
s.size(): 3
elems: 2 3 4

C array and std::array constructors

// (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 or N == extent
    and
  • the conversion from std::remove_pointer_t<decltype(data(arr))> to element_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)
}
Result (console)
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

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)
Undefined Behavior

The behavior is undefined

if:

Overload resolution

These overloads participate in overload resolution only if:

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
}
Result (console)
s.size(): 4
elems: 1 2 3 4

s.size(): 3
elems: 1 2 3

Converting constructor

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()
Undefined Behavior

The behavior is undefined

if:

  • extent != dynamic_extent
    and
  • source.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 is true
      and
  • the conversion from U to element_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);
}
Result (console)
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

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);
}
Result (console)
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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.