template<class T>
Corrade::Containers::ArrayView class

Array view with size information.

Immutable wrapper around continuous range of data. Unlike Array this class doesn't do any memory management. Main use case is passing array along with size information to functions etc. If T is const type, the class is implicitly constructible also from const references to Array and ArrayView of non-const types.

Usage example:

// `a` gets implicitly converted to const array view
void printArray(Containers::ArrayView<const float> values);
Containers::Array<float> a;
printArray(a);

// Wrapping compile-time array with size information
constexpr const int data[]{ 5, 17, -36, 185 };
Containers::ArrayView<const int> b = data; // b.size() == 4

// Wrapping general array with size information
const int* data2;
Containers::ArrayView<const int> c{data2, 3};

Public types

using Type = T
Element type.

Constructors, destructors, conversion operators

ArrayView(std::nullptr_t) constexpr noexcept
Conversion from nullptr
ArrayView() constexpr noexcept
Default constructor.
ArrayView(T* data, std::size_t size) constexpr noexcept
Construct view on an array with explicit length.
template<class U, std::size_t size>
ArrayView(U(&data)[size]) constexpr noexcept
Construct view on a fixed-size array.
template<class U>
ArrayView(ArrayView<U> view) constexpr noexcept
Construct view on ArrayView.
template<std::size_t size, class U>
ArrayView(StaticArrayView<size, U> view) constexpr noexcept
Construct view on StaticArrayView.
operator bool() const explicit
Whether the array is non-empty.
operator T*() const constexpr
Conversion to array type.

Public functions

auto data() const -> T* constexpr
Array data.
auto size() const -> std::size_t constexpr
Array size.
auto empty() const -> bool constexpr
Whether the array is empty.
auto begin() const -> T* constexpr
Pointer to first element.
auto cbegin() const -> T* constexpr
auto end() const -> T*
Pointer to (one item after) last element.
auto cend() const -> T*
auto front() const -> T&
First element.
auto back() const -> T&
Last element.
auto slice(T* begin, T* end) const -> ArrayView<T>
Array slice.
auto slice(std::size_t begin, std::size_t end) const -> ArrayView<T>
template<std::size_t viewSize>
auto slice(T* begin) const -> StaticArrayView<viewSize, T>
Fixed-size array slice.
template<std::size_t viewSize>
auto slice(std::size_t begin) const -> StaticArrayView<viewSize, T>
auto prefix(T* end) const -> ArrayView<T>
Array prefix.
auto prefix(std::size_t end) const -> ArrayView<T>
auto suffix(T* begin) const -> ArrayView<T>
Array suffix.
auto suffix(std::size_t begin) const -> ArrayView<T>

Function documentation

template<class T>
Corrade::Containers::ArrayView<T>::ArrayView() constexpr noexcept

Default constructor.

Creates empty view. Copy non-empty Array or ArrayView onto the instance to make it useful.

template<class T>
Corrade::Containers::ArrayView<T>::ArrayView(T* data, std::size_t size) constexpr noexcept

Construct view on an array with explicit length.

Parameters
data Data pointer
size Data size

template<class T> template<class U, std::size_t size>
Corrade::Containers::ArrayView<T>::ArrayView(U(&data)[size]) constexpr noexcept

Construct view on a fixed-size array.

Parameters
data Fixed-size array

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size.

template<class T> template<class U>
Corrade::Containers::ArrayView<T>::ArrayView(ArrayView<U> view) constexpr noexcept

Construct view on ArrayView.

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size.

template<class T> template<std::size_t size, class U>
Corrade::Containers::ArrayView<T>::ArrayView(StaticArrayView<size, U> view) constexpr noexcept

Construct view on StaticArrayView.

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size.

template<class T>
T* Corrade::Containers::ArrayView<T>::begin() const constexpr

Pointer to first element.

template<class T>
T* Corrade::Containers::ArrayView<T>::cbegin() const constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T>
T* Corrade::Containers::ArrayView<T>::end() const

Pointer to (one item after) last element.

template<class T>
T* Corrade::Containers::ArrayView<T>::cend() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T>
T& Corrade::Containers::ArrayView<T>::front() const

First element.

Expects there is at least one element.

template<class T>
T& Corrade::Containers::ArrayView<T>::back() const

Last element.

Expects there is at least one element.

template<class T>
ArrayView<T> Corrade::Containers::ArrayView<T>::slice(T* begin, T* end) const

Array slice.

Both arguments are expected to be in range.

template<class T>
ArrayView<T> Corrade::Containers::ArrayView<T>::slice(std::size_t begin, std::size_t end) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T> template<std::size_t viewSize>
StaticArrayView<viewSize, T> Corrade::Containers::ArrayView<T>::slice(T* begin) const

Fixed-size array slice.

Both begin and begin + viewSize are expected to be in range.

template<class T> template<std::size_t viewSize>
StaticArrayView<viewSize, T> Corrade::Containers::ArrayView<T>::slice(std::size_t begin) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T>
ArrayView<T> Corrade::Containers::ArrayView<T>::prefix(T* end) const

Array prefix.

Equivalent to data.slice(data.begin(), end). If end is nullptr, returns zero-sized nullptr array.

template<class T>
ArrayView<T> Corrade::Containers::ArrayView<T>::prefix(std::size_t end) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T>
ArrayView<T> Corrade::Containers::ArrayView<T>::suffix(T* begin) const

Array suffix.

Equivalent to data.slice(begin, data.end()). If begin is nullptr and the original array isn't, returns zero-sized nullptr array.

template<class T>
ArrayView<T> Corrade::Containers::ArrayView<T>::suffix(std::size_t begin) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T> template<class T, class D>
ArrayView<T> arrayView(Array<T, D>& array)

Make view on Array.

Convenience alternative to calling Array::operator ArrayView<U>() explicitly. The following two lines are equivalent:

Containers::Array<std::uint32_t> data;

Containers::ArrayView<std::uint32_t> a{data};
auto b = Containers::arrayView(data);

template<class T> template<class T, class D>
ArrayView<const T> arrayView(const Array<T, D>& array)

Make view on const Array.

Convenience alternative to calling Array::operator ArrayView<U>() explicitly. The following two lines are equivalent:

const Containers::Array<std::uint32_t> data;

Containers::ArrayView<const std::uint32_t> a{data};
auto b = Containers::arrayView(data);

template<class T> template<class T>
ArrayView<T> arrayView(T* data, std::size_t size) constexpr

Make view on an array of specific length.

Convenience alternative to ArrayView::ArrayView(T*, std::size_t). The following two lines are equivalent:

std::uint32_t* data;

Containers::ArrayView<std::uint32_t> a{data, 5};
auto b = Containers::arrayView(data, 5);

template<class T> template<std::size_t size, class T>
ArrayView<T> arrayView(T(&data)[size]) constexpr

Make view on fixed-size array.

Convenience alternative to ArrayView::ArrayView(U(&)[size]). The following two lines are equivalent:

std::uint32_t data[15];

Containers::ArrayView<std::uint32_t> a{data};
auto b = Containers::arrayView(data);

template<class T> template<std::size_t size, class T>
ArrayView<T> arrayView(StaticArrayView<size, T> view) constexpr

Make view on StaticArrayView.

Convenience alternative to ArrayView::ArrayView(StaticArrayView<size, U>). The following two lines are equivalent:

std::uint32_t data[15];

Containers::ArrayView<std::uint32_t> a{data};
auto b = Containers::arrayView(data);

template<class T> template<class U, class T>
ArrayView<U> arrayCast(ArrayView<T> view)

Reinterpret-cast an array view.

Size of the new array is calculated as view.size()*sizeof(T)/sizeof(U). Expects that both types are standard layout and the total byte size doesn't change. Example usage:

std::int32_t data[15];
auto a = Containers::arrayView(data); // a.size() == 15
auto b = Containers::arrayCast<char>(a); // b.size() == 60

template<class T> template<class T>
std::size_t arraySize(ArrayView<T> view)

Array view size.

Alias to ArrayView::size(), useful as a shorthand in cases like this:

std::int32_t a[5];

std::size_t size = Containers::arraySize(a); // size == 5