C++20 new feature - std::span
std::span was introduced as a new feature in C++20
std::span provides an efficient way to view a contiguous sequence of objects.
span class is defined as below
1 | template< |
syntax
1 | std::span<type> span_name; |
usage
1 | //1. from an array |
more member functions
- data(): A pointer to the first element of the sequence.
- size(): Returns the size of the sequence that is the number of elements in the sequence.
- empty(): Returns a boolean value that indicates whether the sequence is empty.
- front(): A reference to the first element of the sequence.
- back(): A reference to the last element of the sequence.
- operator[]: An accessor that returns a reference to the element at the specified index.
- at(): An accessor that returns a reference to the element at the specified index, throwing an exception if the index is out of bounds.
- begin(): A function that returns an iterator to the beginning of the sequence.
- end(): A function that returns an iterator to the end of the sequence.
example
1 |
|
motivations for using std::span
Non-owning View: std::span does not own the data it points to. It simply provides a view over an existing sequence, allowing you to work with the data without creating a copy. This can improve performance and memory efficiency, especially when dealing with large amounts of data.
Safety: When you use std::span, you can perform bounds checking and ensure that you do not inadvertently access elements beyond the bounds of the underlying data.
Slicing: You can create sub-spans from an existing std::span, allowing you to work with specific portions of the data. This can be useful when you want to process only a part of a larger data set.
references
C++20 new feature - std::span