A C++ template library for embedded applications
MIT licensed
Join the ETL community
Designed and
maintained by
John Wellbelove
Like the ETL? Become a patron!

span


This class implements a view in to a range of a C array, etl::array, std::array, etl::vector and std::vector. It will
support construction from any class that supports data() and size() member functions as well as plain C arrays.

STL equivalent: std::span.

etl::span<typename T, size_t EXTENT = etl::dynamic_range>
____________________________________________________________________________________________________

Member types


element_type            T
value_type              remove_cv<T>::type
size_type               std::size_t
difference_type         std::ptrdiff_t
reference               value_type&
const_reference         const value_type&
pointer                 value_type*
const_pointer           const value_type*
iterator                Random access iterator
const_iterator          Constant random access iterator
reverse_iterator        std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>

____________________________________________________________________________________________________

Constructors


ETL_CONSTEXPR span()

Default constructor.
____________________________________________________________________________________________________

template <typename TArray>
ETL_CONSTEXPR explicit span(TArray& a)

template <typename TArray>
ETL_CONSTEXPR explicit span(const TArray& a)

Construct from array or vector like class.
____________________________________________________________________________________________________

template <typename TIterator>
ETL_CONSTEXPR span(TIterator begin, TIterator end)

Construct from an iterator or pointer range.
____________________________________________________________________________________________________

template <typename TIterator, typename TSize>
ETL_CONSTEXPR span(TIterator begin, TSize size)

Construct from a start and size
___________________________________________________________________________________________________

template<const size_t ARRAY_SIZE>
ETL_CONSTEXPR explicit span(T(&begin)[ARRAY_SIZE])

Construct from a compile time sized C array.
____________________________________________________________________________________________________

ETL_CONSTEXPR span(const span& other)

Copy constructor.

____________________________________________________________________________________________________

Access


ETL_CONSTEXPR reference operator[](size_t i) const

Returns a reference to the indexed element.
Index out of range results in undefined behaviour.
____________________________________________________________________________________________________

ETL_CONSTEXPR reference front() const ETL_NOEXCEPT

Returns a reference to the first element.
____________________________________________________________________________________________________

ETL_CONSTEXPR reference back() const ETL_NOEXCEPT

Returns a reference to the last element.
____________________________________________________________________________________________________

ETL_CONSTEXPR pointer data() const ETL_NOEXCEPT

Returns a pointer to the first element.
____________________________________________________________________________________________________

ETL_CONSTEXPR14 span& operator =(const span& other) ETL_NOEXCEPT

Assign from a other span
____________________________________________________________________________________________________

template <size_t COUNT>
ETL_CONSTEXPR etl::span<element_type, COUNT> first() const

Returns a span consisting of the first COUNT elements of the current span
____________________________________________________________________________________________________

template <size_t COUNT>
ETL_CONSTEXPR etl::span<element_type, COUNT> last() const

Returns a span consisting of the last COUNT elements of the current span
____________________________________________________________________________________________________

template <size_t OFFSET, size_t COUNT = etl::dynamic_extent>
ETL_CONSTEXPR etl::span<element_type, E> subspan() const

Returns a subspan consisting of the range starting at OFFSET for COUNT elements of the current span

____________________________________________________________________________________________________

Iterators


ETL_CONSTEXPR iterator begin() const ETL_NOEXCEPT

Returns an iterator to the beginning of the span.
____________________________________________________________________________________________________

ETL_CONSTEXPR iterator end() const ETL_NOEXCEPT

Returns an iterator to the end of the span.
____________________________________________________________________________________________________

ETL_CONSTEXPR iterator rbegin() const  ETL_NOEXCEPT

Returns a reverse iterator to the beginning of the span.
____________________________________________________________________________________________________

ETL_CONSTEXPR iterator rend() const ETL_NOEXCEPT

Returns a reverse iterator to the end of the span.

____________________________________________________________________________________________________

Capacity


ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT

Returns true if the size of the span is zero, otherwise false.
____________________________________________________________________________________________________

ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT

Returns the size of the span.

____________________________________________________________________________________________________

ETL_CONSTEXPR size_t size_bytes() const ETL_NOEXCEPT

Returns the size of the span in bytes.

____________________________________________________________________________________________________

Non-member functions


Hash

There is a specialisation of etl::hash for etl::span

____________________________________________________________________________________________________

Example


etl::array<int, 10> data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

using View = etl::span<int>;

View view(data.begin() + 2, data.end() - 2);

void Print(const View& view)
{
  for (size_t i = 0; i < view.size(); ++i)
  {
    std::cout << view[i] << " ";
  }
}

Print(view); // Prints "3 4 5 6 7 8"

auto subview = view.subspan<2, 3>();

Print(subview); // Prints "5 6 7"

size_t hashvalue = etl::hash<View>()(view);
span.h