Skip to main content

ClikaRT::Span

class

Header: ClikaRT/common/span.h
Template: template <``class T``>

ClikaRT::Span<T>: a minimal, C++17-compatible contiguous view, the public stand-in for std::span (which is C++20). The public API uses it so a consumer can build against C++17 (std::string_view / std::optional are already C++17). T carries const-ness: Span<const X> is a read-only view, Span<X> a mutable one, matching the std::span<const X> / std::span<X> the API used before.

At C++20+ it interconverts with std::span for zero-friction interop; a C++17 consumer sees only Span.

A non-owning view over size() contiguous elements of T, the C++17 stand-in for std::span (see the file note above). It never allocates, copies, or owns: the viewed memory must outlive every copy of the view. T's constness is the access law: Span<const X> reads, Span<X> writes through to the caller's memory.

Types

using element_type

using element_type = T

the viewed element type, constness included.

Declared in ClikaRT/common/span.h, line 32

using value_type

using value_type = std::remove_cv_t<T>

element_type with cv stripped.

Declared in ClikaRT/common/span.h, line 33

using size_type

using size_type = std::size_t

the size / index type.

Declared in ClikaRT/common/span.h, line 34

using pointer

using pointer = T*

raw pointer into the viewed memory.

Declared in ClikaRT/common/span.h, line 35

using reference

using reference = T&

reference into the viewed memory.

Declared in ClikaRT/common/span.h, line 36

using iterator

using iterator = T*

iterators are raw pointers.

Declared in ClikaRT/common/span.h, line 37

Member functions

Span()

constexpr constexpr Span() noexcept

An empty view: data() == nullptr, size() == 0.

Declared in ClikaRT/common/span.h, line 40

Span(T, size_type)

constexpr constexpr Span(T* data, size_type size) noexcept

View size elements starting at data; not copied, and the memory must outlive the view.

Declared in ClikaRT/common/span.h, line 42

Span(T, T)

constexpr constexpr Span(T* first, T* last) noexcept

View the half-open range [first, last); not copied.

Declared in ClikaRT/common/span.h, line 44

Span(T(&))

template <``std::size_t N``>
constexpr constexpr Span(T(&) arr``[N]) noexcept

From a C array.

Declared in ClikaRT/common/span.h, line 49

Span(initializer_list<value_type>)

template <``class U = T, class = std::enable_if_t<std::is_const_v<U>>``>
constexpr constexpr Span(std::initializer_list<value_type> il) noexcept

From a braced list at the call site; see the lifetime note above (never store the result).

From a braced initializer list; enables f(t, {1, 5, N, K}) directly at the call site (the list's backing array lives through the full call expression). Only for a read-only Span<const T>. Like c10::ArrayRef, do NOT store the resulting Span: the backing array dies at the end of the statement, so a stored view would dangle; pass it straight as an argument.

The -Winit-list-lifetime suppression is intentional: Span is a non-owning view by contract, and the caller guarantees the backing array outlives the surrounding full-expression (the call-argument idiom above). GCC-only guard: clang has no such warning group and emits no diagnostic for this pattern.

Declared in ClikaRT/common/span.h, line 68

Span(C)

template <``class C``>
constexpr constexpr Span(C&& c)

From any contiguous container with .data() / .size() (std::vector, std::array, …), const-ness permitting (a Span<const X> accepts a container of X; a Span<X> rejects a const container). Not Span itself.

Declared in ClikaRT/common/span.h, line 83

Span(Span<U>)

template <``class U, class = std::enable_if_t<!std::is_same<U, T>::value && std::is_convertible<U (*)[], T (*)[]>::value>``>
constexpr constexpr Span(const Span<U>& other) noexcept

Span<X>Span<const X> (const-adding); the same-type copy is the implicit copy constructor.

Declared in ClikaRT/common/span.h, line 91

data()

constexpr T* data() const noexcept

Pointer to the first viewed element (nullptr when empty).

Declared in ClikaRT/common/span.h, line 101

size()

constexpr size_type size() const noexcept

Number of viewed elements.

Declared in ClikaRT/common/span.h, line 103

empty()

constexpr bool empty() const noexcept

True when size() == 0.

Declared in ClikaRT/common/span.h, line 105

operator[]()

constexpr T& operator[](size_type i) const

The element at i; no bounds check.

Declared in ClikaRT/common/span.h, line 107

front()

constexpr T& front() const

The first element; undefined on an empty view.

Declared in ClikaRT/common/span.h, line 109

back()

constexpr T& back() const

The last element; undefined on an empty view.

Declared in ClikaRT/common/span.h, line 111

begin()

constexpr iterator begin() const noexcept

Iterator at the first element (a raw pointer).

Declared in ClikaRT/common/span.h, line 113

end()

constexpr iterator end() const noexcept

Iterator one past the last element.

Declared in ClikaRT/common/span.h, line 115