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

Array view with size information.

Immutable wrapper around continuous range of data, similar to a dynamic std::span from C++2a. 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. There's also a variant with compile-time size information called StaticArrayView.

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};

STL compatibility

Instances of ArrayView and StaticArrayView are convertible from std::vector and std::array if you include Corrade/Containers/ArrayViewStl.h. The conversion is provided in a separate header to avoid unconditional #include <vector> or #include <array>, which significantly affect compile times. Additionally, the arrayView(T&&) overload also allows for such a conversion. The following table lists allowed conversions:

Corrade typeSTL type
ArrayView<T>std::array<T, size>
ArrayView<const T>const std::array<T>
ArrayView<const void>const std::array<T>
ArrayView<T>std::vector<T, Allocator>
ArrayView<const T>const std::vector<T, Allocator>
ArrayView<const void>const std::vector<T, Allocator>
StaticArrayView<size, T>std::array<T, size>
StaticArrayView<size, const T>const std::array<T, size>

Example:

std::vector<int> a;

Containers::ArrayView<int> b = a;

On compilers that support C++2a and std::span, implicit conversion from and also to it is provided in Corrade/Containers/ArrayViewStlSpan.h. For similar reasons, it's a dedicated header to avoid unconditional #include <span>, but this one is even significantly heavier than the <vector> etc. includes, so it's separate from the others as well. The following table lists allowed conversions:

Corrade typeSTL type
ArrayView<T>std::span<T>
ArrayView<T>std::span<T, size>
ArrayView<const void>std::span<T>
ArrayView<const void>std::span<T, size>
StaticArrayView<size, T>std::span<T, size>
StaticArrayView<size, T>std::span<T>

Example:

std::span<int> a;
Containers::ArrayView<int> b = a;

float c[3]{42.0f, 13.37f, -25.0f};
std::span<float, 3> d = Containers::staticArrayView(c);

Other array classes provide a subset of this STL compatibility as well, see the documentation of Array, StaticArray and StridedArrayView for more information.

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 a view on an array with explicit length.
template<class U, std::size_t size>
ArrayView(U(&data)[size]) constexpr noexcept
Construct a view on a fixed-size array.
template<class U>
ArrayView(ArrayView<U> view) constexpr noexcept
Construct a view on ArrayView.
template<std::size_t size, class U>
ArrayView(StaticArrayView<size, U> view) constexpr noexcept
Construct a view on StaticArrayView.
template<class U, class = decltype(Implementation::ArrayViewConverter<T, typename std::decay<U && >::type>::from(std::declval<U && >()))>
ArrayView(U&& other) constexpr noexcept
Construct a view on an external type / from an external representation.
template<class U, class = decltype(Implementation::ArrayViewConverter<T, U>::to(std::declval<ArrayView<T>>()))>
operator U() const constexpr
Convert the view to external representation.
operator bool() const explicit constexpr
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* constexpr
Pointer to (one item after) last element.
auto cend() const -> T* constexpr
auto front() const -> T&
First element.
auto back() const -> T&
Last element.
auto slice(T* begin, T* end) const -> ArrayView<T> constexpr
Array slice.
auto slice(std::size_t begin, std::size_t end) const -> ArrayView<T> constexpr
template<std::size_t viewSize>
auto slice(T* begin) const -> StaticArrayView<viewSize, T> constexpr
Fixed-size array slice.
template<std::size_t viewSize>
auto slice(std::size_t begin) const -> StaticArrayView<viewSize, T> constexpr
auto prefix(T* end) const -> ArrayView<T> constexpr
Array prefix.
auto prefix(std::size_t end) const -> ArrayView<T> constexpr
Array prefix.
template<std::size_t viewSize>
auto prefix() const -> StaticArrayView<viewSize, T> constexpr
Fixed-size array prefix.
auto suffix(T* begin) const -> ArrayView<T> constexpr
Array suffix.
auto suffix(std::size_t begin) const -> ArrayView<T> constexpr
Array suffix.

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 a 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 a 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 a 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 a view on StaticArrayView.

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

template<class T> template<class U, class = decltype(Implementation::ArrayViewConverter<T, typename std::decay<U && >::type>::from(std::declval<U && >()))>
Corrade::Containers::ArrayView<T>::ArrayView(U&& other) constexpr noexcept

Construct a view on an external type / from an external representation.

template<class T> template<class U, class = decltype(Implementation::ArrayViewConverter<T, U>::to(std::declval<ArrayView<T>>()))>
Corrade::Containers::ArrayView<T>::operator U() const constexpr

Convert the view to external representation.

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 constexpr

Pointer to (one item after) last element.

template<class T>
T* Corrade::Containers::ArrayView<T>::cend() 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>::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 constexpr

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 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> template<std::size_t viewSize>
StaticArrayView<viewSize, T> Corrade::Containers::ArrayView<T>::slice(T* begin) const constexpr

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 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>
ArrayView<T> Corrade::Containers::ArrayView<T>::prefix(T* end) const constexpr

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 constexpr

Array prefix.

Equivalent to data.slice(0, end).

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

Fixed-size array prefix.

Equivalent to data.slice<viewSize>(data.begin()).

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

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 constexpr

Array suffix.

Equivalent to data.slice(begin, _size).

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 a 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 a 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 a 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 T>
ArrayView<T> arrayView(ArrayView<T> view) constexpr

Make a view on a view.

Equivalent to the implicit ArrayView copy constructor — it shouldn't be an error to call arrayView() on itself.

template<class T> template<class T, class U = decltype(Implementation::ErasedArrayViewConverter<typename std::remove_reference<T && >::type>::from(std::declval<T && >()))>
U arrayView(T&& other) constexpr

Make a view on an external type / from an external representation.

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) constexpr

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