template<class T, class D = void(*)(T*, std::size_t)>
Corrade::Containers::Array class

Array wrapper with size information.

Template parameters
T Element type
D Deleter type. Defaults to pointer to void(T*, std::size_t) function, where first is array pointer and second array size

Provides movable RAII wrapper around plain C array. Main use case is storing binary data of unspecified type, where addition/removal of elements is not needed or harmful.

However, the class is usable also as a lighter non-copyable alternative to std::vector; usable in STL algorithms in the same way as plain C array and additionally also in range-for cycle.

Usage example:

// Create default-initialized array with 5 integers and set them to some value
Containers::Array<int> a{5};
int b = 0;
for(auto& i: a) i = b++; // a = {0, 1, 2, 3, 4}

// Create array from given values
auto b = Containers::Array<int>::from(3, 18, -157, 0);
b[3] = 25; // b = {3, 18, -157, 25}

Array initialization

The array is by default default-initialized, which means that trivial types are not initialized at all and default constructor is called on other types. It is possible to initialize the array in a different way using so-called tags:

Example:

// These are equivalent
Containers::Array<int> a1{5};
Containers::Array<int> a2{Containers::DefaultInit, 5};

// Array of 100 zeros
Containers::Array<int> b{Containers::ValueInit, 100};

// Array of type with no default constructor
struct Vec3 {
    Vec3(float, float, float);
};
Containers::Array<Vec3> c{Containers::DirectInit, 5, 5.2f, 0.4f, 1.0f};

// Manual construction of each element
struct Foo {
    Foo(int index);
};
Containers::Array<Foo> d{Containers::NoInit, 5};
int index = 0;
for(Foo& f: d) new(&f) Foo(index++);

Wrapping externally allocated arrays

By default the class makes all allocations using operator new[] and deallocates using operator delete[] for given T, with some additional trickery done internally to make the Array(NoInitT, std::size_t) and Array(DirectInitT, std::size_t, Args&&... args) constructors work. When wrapping an externally allocated array using Array(T*, std::size_t, D), it is possible to specify which function to use for deallocation. By default the deleter is set to nullptr, which is equivalent to deleting the contents using operator delete[].

For example, properly deallocating array allocated using std::malloc():

const int* data = reinterpret_cast<int*>(std::malloc(25*sizeof(int)));

// Will call std::free() on destruction
Containers::Array<int> array{data, 25, [](int* data, std::size_t) { std::free(data); }};

By default, plain function pointers are used to avoid having the type affected by the deleter function. If the deleter needs to manage some state, a custom deleter type can be used:

struct UnmapBuffer {
    UnmapBuffer(GLuint id): _id{id} {}
    void operator()(T*, std::size_t) { glUnmapNamedBuffer(_id); }

private:
    GLuint _id;
};

GLuint buffer;
char* data = reinterpret_cast<char*>(glMapNamedBuffer(buffer, GL_READ_WRITE));

// Will unmap the buffer on destruction
Containers::Array<char, UnmapBuffer> array{data, bufferSize, UnmapBuffer{buffer}};

Public types

using Type = T
Element type.
using Deleter = D
Deleter type.

Public static functions

template<class ... U>
static auto from(U&&... values) -> Array<T, D> deprecated
Construct list-initialized array.
static auto zeroInitialized(std::size_t size) -> Array<T, D> deprecated
Construct value-initialized array.

Constructors, destructors, conversion operators

Array(std::nullptr_t) noexcept
Conversion from nullptr.
Array() noexcept
Default constructor.
Array(DefaultInitT, std::size_t size) explicit
Construct default-initialized array.
Array(ValueInitT, std::size_t size) explicit
Construct value-initialized array.
Array(NoInitT, std::size_t size) explicit
Construct the array without initializing its contents.
template<class... Args>
Array(DirectInitT, std::size_t size, Args&&... args) explicit
Construct direct-initialized array.
Array(InPlaceInitT, std::initializer_list<T> list) explicit
Construct list-initialized array.
Array(std::size_t size) explicit
Construct default-initialized array.
Array(T* data, std::size_t size, D deleter = Implementation::DefaultDeleter<D>{}()) explicit
Wrap existing array.
Array(const Array<T, D>&) deleted
Copying is not allowed.
Array(Array<T, D>&& other) noexcept
Move constructor.
operator bool() const explicit
Whether the array is non-empty.
template<class U>
operator ArrayView<U>() noexcept
Convert to ArrayView.
template<class U>
operator ArrayView<const U>() const noexcept
Convert to const ArrayView.
operator ArrayView<const void>() const noexcept
operator T*() &
Conversion to array type.
operator const T*() const &

Public functions

auto operator=(const Array<T, D>&) -> Array<T, D>& deleted
Copying is not allowed.
auto operator=(Array<T, D>&&) -> Array<T, D>& noexcept
Move assignment.
auto data() -> T*
Array data.
auto data() const -> const T*
auto deleter() const -> D
Array deleter.
auto size() const -> std::size_t
Array size.
auto empty() const -> bool
Whether the array is empty.
auto begin() -> T*
Pointer to first element.
auto begin() const -> const T*
auto cbegin() const -> const T*
auto end() -> T*
Pointer to (one item after) last element.
auto end() const -> const T*
auto cend() const -> const T*
auto slice(T* begin, T* end) -> ArrayView<T>
Reference to array slice.
auto slice(const T* begin, const T* end) const -> ArrayView<const T>
auto slice(std::size_t begin, std::size_t end) -> ArrayView<T>
auto slice(std::size_t begin, std::size_t end) const -> ArrayView<const T>
template<std::size_t size>
auto slice(T* begin) -> StaticArrayView<size, T>
Fixed-size array slice.
template<std::size_t size>
auto slice(const T* begin) const -> StaticArrayView<size, const T>
template<std::size_t size>
auto slice(std::size_t begin) -> StaticArrayView<size, T>
template<std::size_t size>
auto slice(std::size_t begin) const -> StaticArrayView<size, const T>
auto prefix(T* end) -> ArrayView<T>
Array prefix.
auto prefix(const T* end) const -> ArrayView<const T>
auto prefix(std::size_t end) -> ArrayView<T>
auto prefix(std::size_t end) const -> ArrayView<const T>
auto suffix(T* begin) -> ArrayView<T>
Array suffix.
auto suffix(const T* begin) const -> ArrayView<const T>
auto suffix(std::size_t begin) -> ArrayView<T>
auto suffix(std::size_t begin) const -> ArrayView<const T>
auto release() -> T*
Release data storage.

Function documentation

template<class T, class D> template<class ... U>
static Array<T, D> Corrade::Containers::Array<T, D>::from(U&&... values)

Construct list-initialized array.

template<class T, class D>
static Array<T, D> Corrade::Containers::Array<T, D>::zeroInitialized(std::size_t size)

Construct value-initialized array.

template<class T, class D>
Corrade::Containers::Array<T, D>::Array() noexcept

Default constructor.

Creates zero-sized array. Move array with nonzero size onto the instance to make it useful.

template<class T, class D>
Corrade::Containers::Array<T, D>::Array(DefaultInitT, std::size_t size) explicit

Construct default-initialized array.

Creates array of given size, the contents are default-initialized (i.e. builtin types are not initialized). If the size is zero, no allocation is done.

template<class T, class D>
Corrade::Containers::Array<T, D>::Array(ValueInitT, std::size_t size) explicit

Construct value-initialized array.

Creates array of given size, the contents are value-initialized (i.e. builtin types are zero-initialized). For other than builtin types this is the same as Array(std::size_t). If the size is zero, no allocation is done.

Useful if you want to create an array of primitive types and sett them to zero.

template<class T, class D>
Corrade::Containers::Array<T, D>::Array(NoInitT, std::size_t size) explicit

Construct the array without initializing its contents.

Creates array of given size, the contents are not initialized. If the size is zero, no allocation is done. Initialize the values using placement new.

Useful if you will be overwriting all elements later anyway.

template<class T, class D> template<class... Args>
Corrade::Containers::Array<T, D>::Array(DirectInitT, std::size_t size, Args&&... args) explicit

Construct direct-initialized array.

Allocates the array using the Array(NoInitT, std::size_t) constructor and then initializes each element with placement new using forwarded args.

template<class T, class D>
Corrade::Containers::Array<T, D>::Array(InPlaceInitT, std::initializer_list<T> list) explicit

Construct list-initialized array.

Allocates the array using the Array(NoInitT, std::size_t) constructor and then copy-initializes each element with placement new using values from list.

template<class T, class D>
Corrade::Containers::Array<T, D>::Array(std::size_t size) explicit

Construct default-initialized array.

Alias to Array(DefaultInitT, std::size_t).

template<class T, class D>
Corrade::Containers::Array<T, D>::Array(T* data, std::size_t size, D deleter = Implementation::DefaultDeleter<D>{}()) explicit

Wrap existing array.

Note that the array will be deleted on destruction using given deleter. See class documentation for more information about custom deleters and ArrayView for non-owning array wrapper.

template<class T, class D> template<class U>
Corrade::Containers::Array<T, D>::operator ArrayView<U>() noexcept

Convert to ArrayView.

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

template<class T, class D> template<class U>
Corrade::Containers::Array<T, D>::operator ArrayView<const U>() const noexcept

Convert to const ArrayView.

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

template<class T, class D>
Corrade::Containers::Array<T, D>::operator ArrayView<const void>() const noexcept

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, class D>
Corrade::Containers::Array<T, D>::operator const T*() 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, class D>
const T* Corrade::Containers::Array<T, D>::data() 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, class D>
D Corrade::Containers::Array<T, D>::deleter() const

Array deleter.

If set to nullptr, the contents are deleted using standard operator delete[].

template<class T, class D>
const T* Corrade::Containers::Array<T, D>::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, class D>
const T* Corrade::Containers::Array<T, D>::cbegin() 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, class D>
const T* Corrade::Containers::Array<T, D>::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, class D>
const T* Corrade::Containers::Array<T, D>::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, class D>
ArrayView<T> Corrade::Containers::Array<T, D>::slice(T* begin, T* end)

Reference to array slice.

Equivalent to ArrayView::slice().

template<class T, class D>
ArrayView<const T> Corrade::Containers::Array<T, D>::slice(const T* begin, const 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, class D>
ArrayView<T> Corrade::Containers::Array<T, D>::slice(std::size_t begin, std::size_t end)

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

Fixed-size array slice.

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

template<class T, class D> template<std::size_t size>
StaticArrayView<size, const T> Corrade::Containers::Array<T, D>::slice(const 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, class D> template<std::size_t size>
StaticArrayView<size, T> Corrade::Containers::Array<T, D>::slice(std::size_t begin)

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, class D> template<std::size_t size>
StaticArrayView<size, const T> Corrade::Containers::Array<T, D>::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, class D>
ArrayView<T> Corrade::Containers::Array<T, D>::prefix(T* end)

Array prefix.

Equivalent to ArrayView::prefix().

template<class T, class D>
ArrayView<const T> Corrade::Containers::Array<T, D>::prefix(const 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, class D>
ArrayView<T> Corrade::Containers::Array<T, D>::prefix(std::size_t end)

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, class D>
ArrayView<const T> Corrade::Containers::Array<T, D>::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, class D>
ArrayView<T> Corrade::Containers::Array<T, D>::suffix(T* begin)

Array suffix.

Equivalent to ArrayView::suffix().

template<class T, class D>
ArrayView<const T> Corrade::Containers::Array<T, D>::suffix(const 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, class D>
ArrayView<T> Corrade::Containers::Array<T, D>::suffix(std::size_t begin)

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, class D>
ArrayView<const T> Corrade::Containers::Array<T, D>::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, class D>
T* Corrade::Containers::Array<T, D>::release()

Release data storage.

Returns the data pointer and resets internal state to default. Deleting the returned array is user responsibility.

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

Reinterpret-cast an array.

See arrayCast(ArrayView<T>) for more information.

template<class T, class D> template<class T>
std::size_t arraySize(const Array<T>& view)

Array size.

See arraySize(ArrayView<T>) for more information.