6.0.0-rc4Source

QSpan Class

template <typename T, std::size_t E = q20::dynamic_extent> class QSpan

The QSpan class provides a non-owning view of a contiguous sequence of elements. More…

Header#include <QSpan>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QSpan()
QSpan(T *first, qsizetype count)
QSpan(T *first, End last)
QSpan(T (&array)[N])
QSpan(std::array<U, N> &array)
QSpan(const std::array<U, N> &array)
QSpan(C &&c)
QSpan(QSpan<U, F> other)
qsizetypesize() const
qsizetypecount() const
qsizetypelength() const
qsizetypesize_bytes() const
boolisEmpty() const
boolempty() const
T *data() const
T &operator[](qsizetype i) const
T &at(qsizetype i) const
T &front() const
T &back() const
iteratorbegin() const
iteratorend() const
const_iteratorcbegin() const
const_iteratorcend() const
reverse_iteratorrbegin() const
reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const
QSpan<T>first(qsizetype c) const
QSpan<T>last(qsizetype c) const
QSpan<T>subspan(qsizetype pos) const
QSpan<T>subspan(qsizetype pos, qsizetype c) const
QSpan<T>sliced(qsizetype pos) const
QSpan<T>sliced(qsizetype pos, qsizetype c) const
QSpan<T>chopped(qsizetype c) const
voidchop(qsizetype c)
voidslice(qsizetype pos)
voidslice(qsizetype pos, qsizetype c)

Static Public Members

std::size_textent

Detailed Description

A QSpan holds a pointer and an element count. It does not own or copy the elements, so the sequence it refers to must outlive the span. Spans are cheap to copy and are normally passed by value. A QSpan<const T> gives read-only access, and a QSpan<T> lets the elements be modified.

A QSpan can be constructed from a pointer and a count, a pair of pointers, a C array, a std::array, or any container providing data() and size() such as QList or std::vector:

QList<int> values = {1, 2, 3, 4};
QSpan<const int> view(values);
int sum = 0;
for (int v : view.first(2))
    sum += v;

Differences from Qt

The extent is not enforced: every QSpan stores its size at run time, and first(), last() and the other slicing functions always return a dynamic-extent QSpan<T>, as do the free functions as_bytes() and as_writable_bytes(). Indexes and counts are not bounds-checked.

Member Type Documentation

typedef QSpan::const_iterator

The constant iterator type, a plain pointer const T *.

typedef QSpan::const_pointer

A pointer to a constant element, const T *.

typedef QSpan::const_reference

A reference to a constant element, const T &.

typedef QSpan::const_reverse_iterator

The constant reverse iterator type.

typedef QSpan::difference_type

The signed type used for the distance between iterators, qptrdiff.

typedef QSpan::element_type

The type of the elements, T, including any const qualifier.

typedef QSpan::iterator

The iterator type, a plain pointer T *.

typedef QSpan::pointer

A pointer to an element, T *.

typedef QSpan::reference

A reference to an element, T &.

typedef QSpan::reverse_iterator

The reverse iterator type.

typedef QSpan::size_type

The signed type used for sizes and indexes, qsizetype.

typedef QSpan::value_type

The type of the elements with any const or volatile qualifier removed.

Member Function Documentation

[constexpr] QSpan::QSpan()

Constructs an empty span that refers to no elements.

[constexpr] QSpan::QSpan(T *first, qsizetype count)

Constructs a span over count elements starting at first.

template <typename End, typename = std::enable_if_t<std::is_pointer_v<End> && std::is_convertible_v<End, T *>>>[constexpr] QSpan::QSpan(T *first, End last)

This is an overloaded function.

Constructs a span over the elements in the range from first up to, but not including, last.

template <std::size_t N>[constexpr] QSpan::QSpan(T (&array)[N])

This is an overloaded function.

Constructs a span over the elements of the C array array.

template <typename U, std::size_t N, typename = std::enable_if_t<std::is_convertible_v<U (*)[], T (*)[]>>>[constexpr] QSpan::QSpan(std::array<U, N> &array)

This is an overloaded function.

Constructs a span over the elements of the std::array array.

template <typename U, std::size_t N, typename = std::enable_if_t<std::is_convertible_v<const U (*)[], T (*)[]>>>[constexpr] QSpan::QSpan(const std::array<U, N> &array)

This is an overloaded function.

Constructs a span over the elements of the constant std::array array.

template <typename C, typename = std::enable_if_t< !std::is_array_v<std::remove_reference_t<C>> && std::is_convertible_v<std::remove_pointer_t<decltype(std::declval<C &>().data())> (*)[], T (*)[]>>, typename = decltype(std::declval<C &>().size())>[constexpr] QSpan::QSpan(C &&c)

This is an overloaded function.

Constructs a span over the elements of the container c, which must provide data() and size().

template <typename U, std::size_t F, typename = std::enable_if_t<std::is_convertible_v<U (*)[], T (*)[]>>>[constexpr] QSpan::QSpan(QSpan<U, F> other)

This is an overloaded function.

Constructs a span from the span other, whose element type is convertible to T, for example from QSpan<int> to QSpan<const int>.

[constexpr] T & QSpan::at(qsizetype i) const

Returns a reference to the element at index i. The index must be valid; it is not checked.

This is equivalent to the subscript operator.

[constexpr] T & QSpan::back() const

Returns a reference to the last element. The span must not be empty.

See also front().

[constexpr] iterator QSpan::begin() const

Returns an iterator to the first element.

See also end().

[constexpr] const_iterator QSpan::cbegin() const

Returns a constant iterator to the first element.

See also cend().

[constexpr] const_iterator QSpan::cend() const

Returns a constant iterator to the position after the last element.

See also cbegin().

[constexpr] void QSpan::chop(qsizetype c)

Removes the last c elements from the span.

This function was introduced in 6.9.

See also chopped() and slice().

[constexpr] QSpan<T> QSpan::chopped(qsizetype c) const

Returns a span over all but the last c elements.

See also first() and last().

[constexpr] qsizetype QSpan::count() const

Returns the number of elements in the span; the same as size().

[constexpr] const_reverse_iterator QSpan::crbegin() const

Returns a const reverse iterator pointing to the last element of the span.

See also crend() and rbegin().

[constexpr] const_reverse_iterator QSpan::crend() const

Returns a const reverse iterator pointing just before the first element of the span.

See also crbegin() and rend().

[constexpr] T * QSpan::data() const

Returns a pointer to the first element.

[constexpr] bool QSpan::empty() const

Returns true if the span has no elements; the same as isEmpty().

[constexpr] iterator QSpan::end() const

Returns an iterator to the position after the last element.

See also begin().

[constexpr] QSpan<T> QSpan::first(qsizetype c) const

Returns a span over the first c elements. c must not exceed size().

See also last() and subspan().

[constexpr] T & QSpan::front() const

Returns a reference to the first element. The span must not be empty.

See also back().

[constexpr] bool QSpan::isEmpty() const

Returns true if the span has no elements; otherwise returns false.

See also size().

[constexpr] QSpan<T> QSpan::last(qsizetype c) const

Returns a span over the last c elements. c must not exceed size().

See also first() and subspan().

[constexpr] qsizetype QSpan::length() const

Returns the number of elements in the span; the same as size().

[constexpr] T & QSpan::operator[](qsizetype i) const

Returns a reference to the element at index i. The index must be valid; it is not checked.

See also at().

[constexpr] reverse_iterator QSpan::rbegin() const

Returns a reverse iterator to the last element.

See also rend().

[constexpr] reverse_iterator QSpan::rend() const

Returns a reverse iterator to the position before the first element.

See also rbegin().

[constexpr] qsizetype QSpan::size() const

Returns the number of elements in the span.

See also size_bytes() and isEmpty().

[constexpr] qsizetype QSpan::size_bytes() const

Returns the size of the viewed elements in bytes.

See also size().

[constexpr] void QSpan::slice(qsizetype pos)

Removes the elements before index pos from the span.

This function was introduced in 6.9.

See also sliced() and chop().

[constexpr] void QSpan::slice(qsizetype pos, qsizetype c)

This is an overloaded function.

Makes the span refer to c elements starting at index pos.

This function was introduced in 6.9.

[constexpr] QSpan<T> QSpan::sliced(qsizetype pos) const

Returns a span over the elements from index pos to the end; the same as subspan().

[constexpr] QSpan<T> QSpan::sliced(qsizetype pos, qsizetype c) const

This is an overloaded function.

Returns a span over c elements starting at index pos; the same as subspan().

[constexpr] QSpan<T> QSpan::subspan(qsizetype pos) const

Returns a span over the elements from index pos to the end.

See also first(), last() and sliced().

[constexpr] QSpan<T> QSpan::subspan(qsizetype pos, qsizetype c) const

This is an overloaded function.

Returns a span over c elements starting at index pos.

Member Variable Documentation

[static, constexpr] std::size_t QSpan::extent

The extent given as the second template argument, or q20::dynamic_extent. It does not constrain the size of the span.