template<std::size_t size_, class T>
Corrade::Containers::StaticArrayView class

Compile-time-sized array view.

Like ArrayView, but with compile-time size information. Similar to a fixed-size std::span from C++2a. Implicitly convertible to an ArrayView, explicitly convertible from it using the slicing APIs. An owning version of this container is a StaticArray.

Usage

The general API is similar to what's shown in ArrayView usage docs, except that here are additional compile-time overloads of slice(), sliceSize(), prefix(), suffix(), exceptPrefix() and exceptSuffix().

Containers::ArrayView<int> data;

// Take elements 7 to 11
Containers::StaticArrayView<5, int> fiveInts = data.slice<5>(7);

// Convert back to ArrayView
Containers::ArrayView<int> fiveInts2 = data; // fiveInts2.size() == 5
Containers::ArrayView<int> threeInts = data.slice(2, 5);

C++17 structured bindings

If Corrade/Containers/StructuredBindings.h is included, the class can be used in C++17 structured bindings. While the get<i>() overloads are defined inside StaticArrayView itself, a separate header is used for the std::tuple_size and std::tuple_element template specializations, as those may require #include <utility> on some STL implementations. Example:

#include <Corrade/Containers/StructuredBindings.h>



auto [a, b, c] = Containers::ArrayView3<int>{};

STL compatibility

See ArrayView STL compatibility for more information.

Public types

enum (anonymous): std::size_t { Size = size_ }
using Type = T
Element type.

Constructors, destructors, conversion operators

StaticArrayView(std::nullptr_t = nullptr) constexpr noexcept
Default constructor.
StaticArrayView(T* data) explicit constexpr noexcept
Construct a static view on an array.
template<class U>
StaticArrayView(U(&data)[size_]) constexpr noexcept
Construct a static view on a fixed-size array.
template<class U>
StaticArrayView(StaticArrayView<size_, U> view) constexpr noexcept
Construct a static view on an StaticArrayView.
template<class U, class = decltype(Implementation::StaticArrayViewConverter<size_, T, typename std::decay<U && >::type>::from(std::declval<U && >()))>
StaticArrayView(U&& other) constexpr noexcept
Construct a view on an external type / from an external representation.
template<class U, class = decltype(Implementation::StaticArrayViewConverter<size_, T, U>::to(std::declval<StaticArrayView<size_, T>>()))>
operator U() const constexpr
Convert the view to external representation.
operator bool() const explicit constexpr
Whether the view is non-empty.
operator T*() const constexpr
Conversion to the underlying type.

Public functions

auto data() const -> T* constexpr
View data.
auto size() const -> std::size_t constexpr
View size.
auto isEmpty() const -> bool constexpr new in Git master
Whether the view is empty.
auto empty() const -> bool deprecated in Git master constexpr
Whether the view is empty.
auto begin() const -> T* constexpr
Pointer to the first element.
auto cbegin() const -> T* constexpr
auto end() const -> T* constexpr
Pointer to (one item after) the last element.
auto cend() const -> T* constexpr
auto front() const -> T& constexpr
First element.
auto back() const -> T& constexpr
Last element.
auto operator[](std::size_t i) const -> T& constexpr new in Git master
Element access.
auto slice(T* begin, T* end) const -> ArrayView<T> constexpr
View slice.
auto slice(std::size_t begin, std::size_t end) const -> ArrayView<T> constexpr
auto sliceSize(T* begin, std::size_t size) const -> ArrayView<T> constexpr new in Git master
View slice of given size.
auto sliceSize(std::size_t begin, std::size_t size) const -> ArrayView<T> constexpr new in Git master
template<std::size_t size__>
auto slice(T* begin) const -> StaticArrayView<size__, T> constexpr
Fixed-size view slice.
template<std::size_t size__>
auto slice(std::size_t begin) const -> StaticArrayView<size__, T> constexpr
template<std::size_t begin_, std::size_t end_>
auto slice() const -> StaticArrayView<end_ - begin_, T> constexpr new in 2019.10
Fixed-size view slice.
template<std::size_t begin_, std::size_t size__>
auto sliceSize() const -> StaticArrayView<size__, T> constexpr new in Git master
Fixed-size view slice of given size.
auto prefix(T* end) const -> ArrayView<T> constexpr
View prefix until a pointer.
auto suffix(T* begin) const -> ArrayView<T> constexpr
View suffix after a pointer.
auto prefix(std::size_t size) const -> ArrayView<T> constexpr
View on the first size items.
template<std::size_t size__>
auto prefix() const -> StaticArrayView<size__, T> constexpr
Fixed-size view on the first size_ items.
auto exceptPrefix(std::size_t size__) const -> ArrayView<T> constexpr new in Git master
View except the first size items.
auto suffix(std::size_t begin) const -> ArrayView<T> deprecated in Git master constexpr
View except the first size items.
template<std::size_t size__>
auto exceptPrefix() const -> StaticArrayView<size_ - size__, T> constexpr new in Git master
Fixed-size view except the first size__ items.
template<std::size_t begin_>
auto suffix() const -> StaticArrayView<size_ - begin_, T> deprecated in Git master constexpr
View except the first size items.
auto exceptSuffix(std::size_t size) const -> ArrayView<T> constexpr new in Git master
View except the last size items.
auto except(std::size_t count) const -> ArrayView<T> deprecated in Git master constexpr
View except the last size items.
template<std::size_t size__>
auto exceptSuffix() const -> StaticArrayView<size_ - size__, T> constexpr new in Git master
Fixed-size view except the last size__ items.
template<std::size_t count>
auto except() const -> StaticArrayView<size_ - count, T> deprecated in Git master constexpr
View except the last size items.

Enum documentation

template<std::size_t size_, class T>
enum Corrade::Containers::StaticArrayView<size_, T>::(anonymous): std::size_t

Enumerators
Size

Array view size

Function documentation

template<std::size_t size_, class T>
Corrade::Containers::StaticArrayView<size_, T>::StaticArrayView(std::nullptr_t = nullptr) constexpr noexcept

Default constructor.

Creates a nullptr view. Copy a non-empty StaticArray or StaticArrayView onto the instance to make it useful.

template<std::size_t size_, class T>
Corrade::Containers::StaticArrayView<size_, T>::StaticArrayView(T* data) explicit constexpr noexcept

Construct a static view on an array.

Parameters
data Data pointer

The pointer is assumed to contain at least Size elements, but it can't be checked in any way. Use StaticArrayView(U(&)[size_]) or fixed-size slicing from an ArrayView / Array for more safety.

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

Construct a static 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<std::size_t size_, class T> template<class U>
Corrade::Containers::StaticArrayView<size_, T>::StaticArrayView(StaticArrayView<size_, U> view) constexpr noexcept

Construct a static view on an StaticArrayView.

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

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

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

template<std::size_t size_, class T> template<class U, class = decltype(Implementation::StaticArrayViewConverter<size_, T, U>::to(std::declval<StaticArrayView<size_, T>>()))>
Corrade::Containers::StaticArrayView<size_, T>::operator U() const constexpr

Convert the view to external representation.

template<std::size_t size_, class T>
std::size_t Corrade::Containers::StaticArrayView<size_, T>::size() const constexpr

View size.

template<std::size_t size_, class T>
bool Corrade::Containers::StaticArrayView<size_, T>::isEmpty() const constexpr new in Git master

Whether the view is empty.

template<std::size_t size_, class T>
bool Corrade::Containers::StaticArrayView<size_, T>::empty() const constexpr

Whether the view is empty.

template<std::size_t size_, class T>
T* Corrade::Containers::StaticArrayView<size_, T>::begin() const constexpr

Pointer to the first element.

template<std::size_t size_, class T>
T* Corrade::Containers::StaticArrayView<size_, 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<std::size_t size_, class T>
T* Corrade::Containers::StaticArrayView<size_, T>::end() const constexpr

Pointer to (one item after) the last element.

template<std::size_t size_, class T>
T* Corrade::Containers::StaticArrayView<size_, 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<std::size_t size_, class T>
T& Corrade::Containers::StaticArrayView<size_, T>::front() const constexpr

First element.

Expects there is at least one element.

template<std::size_t size_, class T>
T& Corrade::Containers::StaticArrayView<size_, T>::back() const constexpr

Last element.

Expects there is at least one element.

template<std::size_t size_, class T>
T& Corrade::Containers::StaticArrayView<size_, T>::operator[](std::size_t i) const constexpr new in Git master

Element access.

Expects that i is less than size().

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

View slice.

Both arguments are expected to be in range.

template<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, 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<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, T>::sliceSize(T* begin, std::size_t size) const constexpr new in Git master

View slice of given size.

Equivalent to data.slice(begin, begin + size).

template<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, T>::sliceSize(std::size_t begin, std::size_t size) const constexpr new in Git master

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

template<std::size_t size_, class T> template<std::size_t size__>
StaticArrayView<size__, T> Corrade::Containers::StaticArrayView<size_, T>::slice(T* begin) const constexpr

Fixed-size view slice.

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

template<std::size_t size_, class T> template<std::size_t size__>
StaticArrayView<size__, T> Corrade::Containers::StaticArrayView<size_, 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<std::size_t size_, class T> template<std::size_t begin_, std::size_t end_>
StaticArrayView<end_ - begin_, T> Corrade::Containers::StaticArrayView<size_, T>::slice() const constexpr new in 2019.10

Fixed-size view slice.

Expects (at compile time) that begin_ < end_ and end_ is not larger than Size.

template<std::size_t size_, class T> template<std::size_t begin_, std::size_t size__>
StaticArrayView<size__, T> Corrade::Containers::StaticArrayView<size_, T>::sliceSize() const constexpr new in Git master

Fixed-size view slice of given size.

Expects (at compile time) that begin_ + size_ is not larger than Size.

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

View prefix until a pointer.

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

template<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, T>::suffix(T* begin) const constexpr

View suffix after a pointer.

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

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

View on the first size items.

Equivalent to data.slice(0, size).

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

Fixed-size view on the first size_ items.

Equivalent to data.slice<0, size_>().

template<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, T>::exceptPrefix(std::size_t size__) const constexpr new in Git master

View except the first size items.

Equivalent to data.slice(size, data.size()).

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

View except the first size items.

template<std::size_t size_, class T> template<std::size_t size__>
StaticArrayView<size_ - size__, T> Corrade::Containers::StaticArrayView<size_, T>::exceptPrefix() const constexpr new in Git master

Fixed-size view except the first size__ items.

Equivalent to data.slice<size__, Size>().

template<std::size_t size_, class T> template<std::size_t begin_>
StaticArrayView<size_ - begin_, T> Corrade::Containers::StaticArrayView<size_, T>::suffix() const constexpr

View except the first size items.

template<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, T>::exceptSuffix(std::size_t size) const constexpr new in Git master

View except the last size items.

Equivalent to data.slice(0, data.size() - size).

template<std::size_t size_, class T>
ArrayView<T> Corrade::Containers::StaticArrayView<size_, T>::except(std::size_t count) const constexpr

View except the last size items.

template<std::size_t size_, class T> template<std::size_t size__>
StaticArrayView<size_ - size__, T> Corrade::Containers::StaticArrayView<size_, T>::exceptSuffix() const constexpr new in Git master

Fixed-size view except the last size__ items.

Equivalent to data.slice<0, Size - size__>().

template<std::size_t size_, class T> template<std::size_t count>
StaticArrayView<size_ - count, T> Corrade::Containers::StaticArrayView<size_, T>::except() const constexpr

View except the last size items.

template<std::size_t size_, class T> template<std::size_t size, class T>
StaticArrayView<size, T> staticArrayView(T* data) constexpr

Make a static view on an array.

Convenience alternative to StaticArrayView::StaticArrayView(T*). The following two lines are equivalent:

int* data = ;

Containers::StaticArrayView<5, int> a{data};
auto b = Containers::staticArrayView<5>(data);

template<std::size_t size_, class T> template<std::size_t size, class T>
StaticArrayView<size, T> staticArrayView(T(&data)[size]) constexpr

Make a static view on a fixed-size array.

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

int data[15];

Containers::StaticArrayView<15, int> a{data};
auto b = Containers::staticArrayView(data);

template<std::size_t size_, class T> template<std::size_t size, class T>
StaticArrayView<size, T> staticArrayView(StaticArrayView<size, T> view) constexpr

Make a static view on a view.

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

template<std::size_t size_, class T> template<class T, class U = decltype(Implementation::ErasedStaticArrayViewConverter<typename std::remove_reference<T && >::type>::from(std::declval<T && >()))>
U staticArrayView(T&& other) constexpr

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

template<std::size_t size_, class T> template<class U, std::size_t size, class T>
StaticArrayView<size*sizeof(T)/sizeof(U), U> arrayCast(StaticArrayView<size, T> view)

Reinterpret-cast a static 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:

int data[15];
auto a = Containers::staticArrayView(data); // a.size() == 15
Containers::StaticArrayView<60, char> b = Containers::arrayCast<char>(a);

template<std::size_t size_, class T> template<class U, std::size_t size, class T>
StaticArrayView<size*sizeof(T)/sizeof(U), U> arrayCast(T(&data)[size])

Reinterpret-cast a statically sized array.

Calls arrayCast(StaticArrayView<size, T>) with the argument converted to StaticArrayView of the same type and size. Example usage:

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